create.c

来自「这个是学习嵌入式开发的重要例子」· C语言 代码 · 共 43 行

C
43
字号
#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <semaphore.h>#include <string.h>#include <errno.h>#include <fcntl.h>int main(int argc, char **argv){  if (argc < 2)  {    fprintf(stderr, "Usage: %s <name>\n", argv[0]);    exit(1);  }  sem_t *sem = NULL;  unsigned int value;  value = 1;  //Upon  successful  completion,  the sem_open() function shall return the address of the semaphore. Other-  //wise, it shall return a value of SEM_FAILED and set errno to indicate the error. The  symbol  SEM_FAILED  //is  defined  in  the  <semaphore.h>  header. No successful return from sem_open() shall return the value  //SEM_FAILED.  //sem_t *sem_open(const char *name, int oflag, ...);  if ((sem = sem_open(argv[1], O_CREAT | O_EXCL, 0644, value)) == SEM_FAILED)  {    fprintf(stderr, "sem_open() failed: %s\n", strerror(errno));    exit(1);  }  fprintf(stdout, "sem_open() successed.\n");  //int sem_close(sem_t *sem);  sem_close(sem);  //int sem_unlink(const char *name);  //sem_unlink(argv[1]);  return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?