Skip to content

Instantly share code, notes, and snippets.

@yifu
Created September 21, 2012 15:29
Show Gist options
  • Save yifu/3762184 to your computer and use it in GitHub Desktop.
Save yifu/3762184 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sys/stat.h>
#include <sys/sem.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <errno.h>
#include <fcntl.h>
int main()
{
std::cout << "hello world" << std::endl;
sem_t *mySem;
mySem = sem_open("/name1", O_CREAT | O_EXCL, 0666, 2);
if (mySem == SEM_FAILED)
{
/* Check the error value */
if (errno == EACCES)
{
/* Permission is denied */
printf("Error: EACCESS Permission denied\n");
}
else
{
/* Show the other error */
printf("Some other error");
/* return; */
}
}
else
{
/* Process created the semaphore */
int a1;
sem_wait(mySem);
printf("Sample data written\n");
sem_getvalue(mySem, &a1);
printf("Value of mySem=%d \n", a1);
sem_unlink("/name1");
sem_close(mySem);
exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment