Skip to content

Instantly share code, notes, and snippets.

@umitanuki
Created May 1, 2013 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umitanuki/5496746 to your computer and use it in GitHub Desktop.
Save umitanuki/5496746 to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/sem.h>
typedef void (*sighandler_t)(int);
static void
signal_handler(int sig)
{
fprintf(stderr, "signal caught\n");
return;
}
static sighandler_t
install_sighandler(int signum, sighandler_t handler)
{
struct sigaction act, oact;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
sigaction(signum, &act, &oact);
return oact.sa_handler;
}
int
main(int argc, char *argv[])
{
int semid, res;
struct sembuf sem_op;
install_sighandler(SIGUSR1, signal_handler);
if ((semid = semget((key_t) 42, 1, 0666|IPC_CREAT)) == -1)
{
perror("Failed to create a private semaphore.");
return 1;
}
sem_op.sem_num = 0;
sem_op.sem_op = -1;
sem_op.sem_flg = 0;
res = semop(semid, &sem_op, 1);
if (res == -1)
{
perror("semop failed");
return 1;
}
return 0;
}
PROG = run
OBJS = main.o
all: $(PROG)
$(PROG): $(OBJS)
$(CC) -o $(PROG) $(OBJS)
clean:
rm -f $(PROG) $(OBJS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment