fork(1) download
  1. #include <stdio.h>
  2. #include<sys/types.h>
  3. #include<unistd.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6. #include<fcntl.h>
  7. #include<sys/shm.h>
  8. #include<sys/stat.h>
  9.  
  10. int main() {
  11. // your code goes here
  12. const int nSize = 4096;
  13. const char *pName = "OS";
  14. int shm_fd;
  15. void *ptr = NULL;
  16.  
  17. printf("Please input a positive number\n");
  18. int n;
  19. scanf("%d",&n);
  20. if(n <= 0)
  21. {
  22. printf("Input positive number\n");
  23. return 1;
  24. }
  25.  
  26.  
  27. pid_t pid;
  28. pid = fork();
  29. if(pid < 0)
  30. {
  31. printf("fork error\n");
  32. return 1;
  33. }
  34. else if(pid == 0)
  35. {
  36. shm_fd = shm_open(pName,O_CREAT|O_RDRW,0666);
  37. ftruncate(shm_fd,nSize);
  38. ptr = mmap(0,nSize,PROT_WRITE,MAP_SHARED,shm_fd,0);
  39.  
  40. char buf[10];
  41. sprintf(buf,"%d ",n);
  42. sprintf(ptr,"%s",buf);
  43. ptr += strlen(buf);
  44. while(n > 1)
  45. {
  46. if(n%2 == 0)
  47. {
  48. n /=2;
  49. sprintf(buf,"%d ",n);
  50. sprintf(ptr,"%s",buf);
  51. ptr += strlen(buf);
  52. }
  53. else
  54. {
  55. n =3*n + 1;
  56. sprintf(buf,"%d ",n);
  57. sprintf(ptr,"%s",buf);
  58. ptr += strlen(buf);
  59. }
  60. }
  61. }
  62. else
  63. {
  64. //sleep(10);
  65. int st;
  66. wait(&st);
  67. shm_fd = shm_open(pName,O_RDONLY,0666);
  68. ptr = mmap(0,nSize,PROT_READ,MAP_SHARED,shm_fd,0);
  69. printf("Child[%d] Complete[%d]\n",pid,st);
  70. printf("%s\n",(char *)ptr);
  71. shm_unlink(pName);
  72. }
  73. return 0;
  74. }
  75.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
45
compilation info
prog.c: In function ‘main’:
prog.c:36:12: warning: implicit declaration of function ‘shm_open’; did you mean ‘shmget’? [-Wimplicit-function-declaration]
   shm_fd = shm_open(pName,O_CREAT|O_RDRW,0666);
            ^~~~~~~~
            shmget
prog.c:36:35: error: ‘O_RDRW’ undeclared (first use in this function); did you mean ‘O_RDWR’?
   shm_fd = shm_open(pName,O_CREAT|O_RDRW,0666);
                                   ^~~~~~
                                   O_RDWR
prog.c:36:35: note: each undeclared identifier is reported only once for each function it appears in
prog.c:38:9: warning: implicit declaration of function ‘mmap’ [-Wimplicit-function-declaration]
   ptr = mmap(0,nSize,PROT_WRITE,MAP_SHARED,shm_fd,0);
         ^~~~
prog.c:38:22: error: ‘PROT_WRITE’ undeclared (first use in this function)
   ptr = mmap(0,nSize,PROT_WRITE,MAP_SHARED,shm_fd,0);
                      ^~~~~~~~~~
prog.c:38:33: error: ‘MAP_SHARED’ undeclared (first use in this function)
   ptr = mmap(0,nSize,PROT_WRITE,MAP_SHARED,shm_fd,0);
                                 ^~~~~~~~~~
prog.c:66:3: warning: implicit declaration of function ‘wait’; did you mean ‘main’? [-Wimplicit-function-declaration]
   wait(&st);
   ^~~~
   main
prog.c:68:22: error: ‘PROT_READ’ undeclared (first use in this function)
   ptr = mmap(0,nSize,PROT_READ,MAP_SHARED,shm_fd,0);
                      ^~~~~~~~~
prog.c:71:3: warning: implicit declaration of function ‘shm_unlink’; did you mean ‘unlink’? [-Wimplicit-function-declaration]
   shm_unlink(pName);
   ^~~~~~~~~~
   unlink
prog.c:19:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ^~~~~~~~~~~~~~
prog.c:37:3: warning: ignoring return value of ‘ftruncate’, declared with attribute warn_unused_result [-Wunused-result]
   ftruncate(shm_fd,nSize);
   ^~~~~~~~~~~~~~~~~~~~~~~
stdout
Standard output is empty