Created
May 1, 2013 17:26
-
-
Save umitanuki/5496746 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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