diff --git a/miscellaneous/fork.c b/miscellaneous/fork.c new file mode 100644 index 0000000..49d50a4 --- /dev/null +++ b/miscellaneous/fork.c @@ -0,0 +1,30 @@ +#include +#include +#include + +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; +} diff --git a/miscellaneous/test_wait.c b/miscellaneous/test_wait.c new file mode 100644 index 0000000..258fad7 --- /dev/null +++ b/miscellaneous/test_wait.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +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); +} + diff --git a/tcp/tcp-iterative/tcp-iterative-client.c b/tcp/tcp-iterative/tcp-iterative-client.c new file mode 100644 index 0000000..2a9c8ea --- /dev/null +++ b/tcp/tcp-iterative/tcp-iterative-client.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +#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)); + } +} \ No newline at end of file diff --git a/tcp/tcp-iterative/tcp-iterative-server.c b/tcp/tcp-iterative/tcp-iterative-server.c new file mode 100644 index 0000000..4838f42 --- /dev/null +++ b/tcp/tcp-iterative/tcp-iterative-server.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +#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; +} \ No newline at end of file