Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5.  
  6. int create_new_child(char *text, char *filename);
  7.  
  8. int main(){
  9.  
  10.     int nb_child = 5;
  11.     int pid_array[nb_child];
  12.     for(int i = 0; i < nb_child; i++){
  13.         pid_array[i] = create_new_child("test" , "toto.txt");
  14.         printf("%d\n", pid_array[i]);
  15.     }
  16.  
  17.  
  18.  
  19.  
  20. }
  21.  
  22.  
  23.  
  24. int create_new_child(char *text, char *filename){
  25.     int pid;
  26.     int ret_pid;
  27.     FILE* fp = NULL;
  28.     fp = fopen(filename, "w+");
  29.     pid = fork();
  30.     if(pid != 0){
  31.         printf("%s\n", text);
  32.         fputs(text, fp);
  33.     }
  34.    
  35.     ret_pid = getpid();
  36.    
  37.     return ret_pid;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement