Skip to content

Commit

Permalink
added more codes
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvi-surana committed May 3, 2016
1 parent 81804d3 commit 362e06b
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
30 changes: 30 additions & 0 deletions miscellaneous/fork.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<unistd.h>
#include<stdio.h>
#include<sys/wait.h>

int main(int argc, char const *argv[])
{
int pid = fork();
int status=0;
//int s =wait(&status);
// printf("\nIn code with pid = %d, the first status=%d\t%d\n", getpid(),status,s);
if(pid==0)
{
printf("\nMy id :%d\n", getpid());
sleep(20);
printf("Parent id after sleep:%d\n", getppid());
exit(0);

}
int p=fork();
if(p==0)
exit(0);
int s =wait(&status);
printf("\nIn code with pid = %d, the first status=%d\t%d\n", getpid(),status,s);
while(s>=0)
{ s =wait(&status);
printf("\nIn the second part of the code with pid = %d, the first status=%d\t%d\n", getpid(),status,s);
}

return 0;
}
40 changes: 40 additions & 0 deletions miscellaneous/test_wait.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<sys/types.h>
#include<sys/wait.h>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
pid_t pid;
int status;

if ((pid = fork()) < 0)
printf("fork error");
else if (pid == 0) /* child */
exit(7);

if (wait(&status) != pid) /* wait for child */
printf("\nwait error");
printf("\n status=%d\n",status); /* and print its status */

status=0;
if ((pid = fork()) < 0)
printf("\nfork error");
else if (pid == 0) /* child */
abort(); /* generates SIGABRT */

if (wait(&status) != pid) /* wait for child */
printf("\nwait error");
printf("\n status=%d\n",status); /* and print its status */
status=0;
if ((pid = fork()) < 0)
printf("\nfork error");
else if (pid == 0) /* child */
status /= 0; /* divide by 0 generates SIGFPE */

if (wait(&status) != pid) /* wait for child */
printf("\nwait error");
printf("\n status=%d\n",status); /* and print its status */

exit(0);
}

36 changes: 36 additions & 0 deletions tcp/tcp-iterative/tcp-iterative-client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>

#define SERV_IP "127.0.0.1"
#define SERV_PORT 9002
#define MAX 1001

int main()
{
int sockfd;
struct sockaddr_in server,client;
socklen_t clilen;
char sendline[MAX],recvline[MAX];
sockfd=socket(AF_INET,SOCK_STREAM,0);
int n;

bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_addr.s_addr=inet_addr(SERV_IP);
server.sin_port=htons(SERV_PORT);

bind(sockfd,(struct sockaddr*)&server,sizeof(server));

connect(sockfd,(struct sockaddr*)&server,sizeof(server));
while(fgets(sendline,MAX,stdin)!=NULL)
{
write(sockfd,sendline,strlen(sendline));
n=read(sockfd,recvline,MAX);
recvline[n]=0;
fputs(recvline,stdout);
bzero(&recvline,sizeof(recvline));
bzero(&sendline,sizeof(sendline));
}
}
43 changes: 43 additions & 0 deletions tcp/tcp-iterative/tcp-iterative-server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>

#define PORT 9002
#define SERV_IP "127.0.0.1"
#define MAX 1001

int main()
{
int listenfd,connfd;
struct sockaddr_in server,client;
socklen_t clilen;
char buff[MAX];
int n;

listenfd=socket(AF_INET,SOCK_STREAM,0);

server.sin_family=AF_INET;
server.sin_addr.s_addr=inet_addr(SERV_IP);
server.sin_port=htons(PORT);

bind(listenfd,(struct sockaddr*)&server,sizeof(server));

listen(listenfd,5);

for(;;)
{
clilen=sizeof(client);
connfd=accept(listenfd,(struct sockaddr*)&client,&clilen);
while((n=read(connfd,buff,MAX))>0)
{
printf("Echoing back\n");
write(connfd,buff,n);
}
if(n==0)
close(connfd);
}
close(listenfd);

return 0;
}

0 comments on commit 362e06b

Please sign in to comment.