Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <fcntl.h>
- #define PERMS 0666
- void exitt(int sgc){
- exit(1);
- }
- int main () {
- int pid2, pid3, pid4, pid5, pid6, st;
- //parent
- printf("main parent process %d\n",getpid());
- pid2 = fork();
- //first child
- if (pid2 == 0) {
- int fd;
- char fname[10];
- sprintf(fname, "%d",getpid());
- if((fd, open(fname, O_CREAT | O_WRONLY, PERMS)) < 0 ){
- perror("open file error");
- }
- close(fd);
- printf("first child process%d\n",getpid());
- }
- pid3 = fork();
- //second child
- if (pid3 == 0){
- int fd;
- char fname[10];
- sprintf(fname, "%d",getpid());
- if((fd, open(fname, O_CREAT | O_WRONLY, PERMS)) < 0 ){
- perror("open file error");
- }
- close(fd);
- printf("second child process%d\n", getpid());
- }
- pid4 = fork();
- //third child
- if (pid4 == 0){
- int fd;
- char fname[10];
- sprintf(fname, "%d",getpid());
- if((fd, open(fname, O_CREAT | O_WRONLY, PERMS)) < 0 ){
- perror("open file error");
- }
- close(fd);
- printf("third child process%d\n",getpid());
- }
- pid5 = fork();
- //fourth child
- if (pid5 == 0){
- int fd;
- char fname[10];
- sprintf(fname, "%d",getpid());
- if((fd, open(fname, O_CREAT | O_WRONLY, PERMS)) < 0 ){
- perror("open file error");
- }
- close(fd);
- printf("fourth child process%d\n",getpid());
- }
- pid6 = fork();
- //fifth child
- if (pid6 == 0){
- int fd;
- char fname[10];
- sprintf(fname, "%d",getpid());
- if((fd, open(fname, O_CREAT | O_WRONLY, PERMS)) < 0 ){
- perror("open file error");
- }
- close(fd);
- printf("fifth child process%d\n",getpid());
- signal (SIGINT, exitt);
- }
- if ( pid6 < 0) {
- printf("can't create process 6:error %d\n", pid6);
- signal (SIGINT, exitt);
- kill(pid2,SIGINT);
- sleep(1);
- }
- else
- if (pid5 < 0){
- printf("can't create process 5:error %d\n", pid5);
- signal (SIGINT, exitt);
- kill(pid5,SIGINT);
- }
- else
- if (pid4 < 0){
- printf("can't create process 4:error %d\n", pid4);
- signal (SIGINT, exitt);
- kill(pid4,SIGINT);
- }
- else
- if (pid3 < 0){
- printf("can't create process 3:error %d\n", pid3);
- signal (SIGINT, exitt);
- kill(pid3,SIGINT);
- }
- else
- {
- if (pid2 < 0)
- printf("can't create process 2:error %d\n", pid2);
- signal (SIGINT, exitt);
- kill(pid2,SIGINT);
- }
- wait(&st);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement