-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81804d3
commit 362e06b
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |