fork download
  1. #include <sys/types.h>
  2. #include <linux/ipc.h>
  3. #include <linux/sem.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define SEM_NUM 10
  8. #define SEM_MODE (IPC_CREAT|0660)
  9.  
  10. void printf_stat(union semun *arg);
  11.  
  12. int main(void)
  13. {
  14. int semid;
  15. union semun semopts;
  16. struct semid_ds semds;
  17. if((semid=semget(IPC_PRIVATE,SEM_NUM,SEM_MODE))==-1)
  18. {
  19. fprintf(stderr,"semget error!\n");
  20. exit(1);
  21. }
  22. semopts.buf=&semds;
  23. if(semctl(semid,IPC_STAT,semopts)==-1)
  24. {
  25. fprintf(stderr,"get semid_ds error!\n");
  26. exit(1);
  27. }
  28. printfmode(&semopts);
  29. changemode(semid,0600);
  30. if(semctl(semid,IPC_STAT,semopts)==-1)
  31. {
  32. fprintf(stderr,"get semid_ds error!\n");
  33. exit(1);
  34. }
  35. printfmode(&semopts);
  36. if(semctl(semid,IPC_RMID,0)<0)
  37. {
  38. fprintf(stderr,"semctl error\n");
  39. exit(1);
  40. }
  41. exit(0);
  42. }
  43. void printfmode (union semun *arg)
  44. {
  45. printf(" mode=%d:\n",arg->buf->sem_perm.mode);
  46. return;
  47. }
  48. void changemode(int sid,char *mode)
  49. {
  50. int rc;
  51. union semun semopts;
  52. struct semid_ds mysemds;
  53. semopts.buf=&mysemds;
  54. rc=semctl(sid,0,IPC_STAT,semopts);
  55. if(rc==-1)
  56. {
  57. printf("semctl error!\n");
  58. exit(1);
  59. }
  60. sscanf(mode,"%ho",&semopts.buf->sem_perm.mode);
  61. semctl(sid,0,IPC_SET,semopts);
  62. return;
  63. }
  64.  
Runtime error #stdin #stdout #stderr 0s 1784KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
get semid_ds error!