#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define __USE_GNU
#include <sched.h>
void init_lock(struct flock *f)
{
f->l_type = F_WRLCK; /* write lock set */
f->l_whence = SEEK_SET;
f->l_start = 0;
f->l_len = 0;
f->l_pid = getpid();
}
int lock(int fd, struct flock *f)
{
init_lock(f);
if(fcntl(fd, F_SETLKW, f) == -1) {
fprintf(stderr,"fcntl() failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
int unlock(int fd, struct flock *f)
{
f->l_type = F_UNLCK;
if(fcntl(fd, F_SETLK, f) == -1) {
fprintf(stderr, "fcntl() failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
int file_op(void *arg)
{
char buff[256];
int fd = (int) arg, n;
struct flock my_lock;
printf("Trying to get lock\n");
if(lock(fd, &my_lock) == -1) { /* lock acquired by a thread */
return -1;
}
printf("Got lock: %d\n", getpid()); /* I am printing thread id after lock() */
printf("Enter string to write in file : ");
scanf("%s", buff);
if((n=write(fd, &buff, strlen(buff))) == -1) {
fprintf(stderr, "write() failed: %s\n", strerror(errno));
}
if(unlock(fd, &my_lock) == -1) {
return -1;
}
printf("Lock Released: %d\n", getpid());
return 0;
}
int main()
{
char *stack;
int fd, i=0, cid, stacksize;
if((fd = open("sample.txt", O_CREAT | O_WRONLY | O_APPEND, 0644)) == -1) {
printf("Error in file opening\n");
exit(1);
}
stacksize = 3*1024*1024;
for(i=0; i<5; i++) {
stack = malloc(stacksize);
if((cid = clone(&file_op, stack + stacksize, CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD, (void *) fd)) == -1) {
fprintf(stderr,"clone() failed: %s\n", strerror(errno));
break;
}
}
sleep(30);
close(fd);
return 0;
}
I want that every clone() will wait for lock.
But Output of this code (something like this):
Trying to get lock
Trying to get lock
Trying to get lock
Got lock: Got lock: 10287
Got lock: Got lock: 10287
Enter string to write in file : Trying to get lock
Enter string to wriGot lock: 10287
Got lock: 10287
Got lock: 10287
Enter string to write in file : Trying to get lock
Got lock: 10287
Got lock: Enter string to write in file :
But when i am removing CLONE_FILES field set from clone(2), it goes all well. Other clone threads will wait for a lock().
Output of that:
Trying to get lock
Got lock: 10311
Trying to get lock
Trying to get lock
Trying to get lock
Trying to get lock
Any other alternatives (with CLONE_FILES)? And Why this kind of behavior?
Beginner in this field.
The locking provided by flock is per process, not per thread.
From http://linux.die.net/man/2/flock (emphasis mine):
A call to flock() may block if an incompatible lock is held by another process.
Subsequent flock() calls on an already locked file will convert an existing lock to the new lock mode.
Locks created by flock() are associated with an open file table entry.
Although threads are not explicitly mentioned multiple threads share a file table entry whereas multiple processes do not. Passing CLONE_FILES to clone causes your 'processes' to share file tables.
A solution might be to call dup to make more file descriptors. From the documentation:
If a process uses open(2) (or similar) to obtain more than one descriptor for the same
file, these descriptors are treated independently by flock(). An attempt to lock the file
using one of these file descriptors may be denied by a lock that the calling process has
already placed via another descriptor.
Related
I have a code. There are 2 processes. Parent is writer on file a.txt. Child is reader on a.txt.Parent has 2 threads and child has 2 threads. Parent's 1st thread opens a file parent1.txt . reads 128 chars. writes to a.txt.Parent's 2nd thread opens file parent2.txt.reads 128 chars. writes to a.txt. Child's 1st thread reads 128 chars from a.txt and writes to child1.txt. child's 2nd thread reads 128 chars from a.txt and child2.txt. Any parent thread , after writing, should generate event and invoke the child's reader threads.I have implemented a solution using mutex and condition variable.Parent's writer threads generate pthread_cond_signal after writing to a.txt. 1>But child's reader threads are not running after that. both parent threads are running in loop.2>Parent reads frm parent1.txt. the fread is successfull. But when it writes to a.txt, it is not successfull. the a.txt file is always empty.I think mutex can not be use between multiple processes. That may be 1 problem
My code is as follows
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
FILE*fd,*fdRead1,*fdRead2,*fdWrite1,*fdWrite2;
pthread_mutex_t *mut1;
pthread_mutexattr_t attrmutex;
pthread_cond_t *cond_var;
pthread_condattr_t attrcond;
#define OKTOWRITE "/condwrite"
#define MESSAGE "/msg"
#define MUTEX "/mutex_lock"
void* R1(void *)
{
char buf[128];
int size;
fdWrite1 = fopen("child1.txt","w+");
cout<<"R1Thread"<<endl;
for(int i=0;i<10;i++)
{
cout<<"R1Thread-1"<<endl;
pthread_mutex_lock(mut1);
pthread_cond_wait(cond_var,mut1);
cout<<"R1Thread-2"<<endl;
size = fread(buf,128,1,fd);
fwrite(buf,size,1,fdWrite1);
pthread_mutex_unlock(mut1);
}
fclose(fdWrite1);
}
void* R2(void *)
{
char buf[128];
int size;
fdWrite2 = fopen("child2.txt","w+");
cout<<"R2Thread"<<endl;
for(int i=0;i<10;i++)
{
cout<<"R2Thread-1"<<endl;
pthread_mutex_lock(mut1);
pthread_cond_wait(cond_var,mut1);
cout<<"R2Thread-2"<<endl;
size = fread(buf,128,1,fd);
fwrite(buf,size,1,fdWrite2);
pthread_mutex_unlock(mut1);
}
fclose(fdWrite2);
}
void* W1(void *)
{
char buf[128];
int size;
fdRead1 = fopen("parent1.txt","r");
for(int i=0;i<10;i++)
{
pthread_mutex_lock(mut1);
size = fread(buf,128,1,fdRead1);
fwrite(buf,size,1,fd);
pthread_cond_signal(cond_var);
cout<<"W2Thread-1"<<endl;
pthread_mutex_unlock(mut1);
sleep(10);
}
fclose(fdRead1);
}
void* W2(void *)
{
char buf[128];
int size;
fdRead2 = fopen("parent2.txt","r");
for(int i=0;i<10;i++)
{
pthread_mutex_lock(mut1);
size = fread(buf,128,1,fdRead2);
fwrite(buf,size,1,fd);
pthread_cond_signal(cond_var);
cout<<"W2Thread-1"<<endl;
pthread_mutex_unlock(mut1);
sleep(1000);
}
fclose(fdRead2);
}
int main()
{
int des_cond, des_msg, des_mutex;
int mode = S_IRWXU | S_IRWXG;
des_mutex = shm_open(MUTEX, O_CREAT | O_RDWR | O_TRUNC, mode);
if (des_mutex < 0) {
perror("failure on shm_open on des_mutex");
exit(1);
}
if (ftruncate(des_mutex, sizeof(pthread_mutex_t)) == -1) {
perror("Error on ftruncate to sizeof pthread_cond_t\n");
exit(-1);
}
mut1 = (pthread_mutex_t*) mmap(NULL, sizeof(pthread_mutex_t),
PROT_READ | PROT_WRITE, MAP_SHARED, des_mutex, 0);
if (mut1 == MAP_FAILED ) {
perror("Error on mmap on mutex\n");
exit(1);
}
des_cond = shm_open(OKTOWRITE, O_CREAT | O_RDWR | O_TRUNC, mode);
if (des_cond < 0) {
perror("failure on shm_open on des_cond");
exit(1);
}
if (ftruncate(des_cond, sizeof(pthread_cond_t)) == -1) {
perror("Error on ftruncate to sizeof pthread_cond_t\n");
exit(-1);
}
cond_var = (pthread_cond_t*) mmap(NULL, sizeof(pthread_cond_t),
PROT_READ | PROT_WRITE, MAP_SHARED, des_cond, 0);
if (cond_var == MAP_FAILED ) {
perror("Error on mmap on condition\n");
exit(1);
}
/* Initialise attribute to mutex. */
pthread_mutexattr_init(&attrmutex);
pthread_mutexattr_setpshared(&attrmutex, PTHREAD_PROCESS_SHARED);
/* Allocate memory to pmutex here. */
/* Initialise mutex. */
pthread_mutex_init(mut1, &attrmutex);
/* Initialise attribute to condition. */
pthread_condattr_init(&attrcond);
pthread_condattr_setpshared(&attrcond, PTHREAD_PROCESS_SHARED);
/* Allocate memory to pcond here. */
/* Initialise condition. */
pthread_cond_init(cond_var, &attrcond);
pthread_t thR1,thR2,thW1,thW2;
fd = fopen("a.txt","w+");
int res = fork();
if(res<0) perror("error forking\n");
if(res==0)//child
{
cout<<"child created"<<endl;
pthread_create(&thR1,0,R1,0);
//pthread_create(&thR2,0,R2,0);
pthread_join(thR1,0);
//pthread_join(thR2,0);
fclose(fd);
}
else//parent
{
//fdRead = fopen("parent.txt","r");
pthread_create(&thW1,0,W1,0);
//pthread_create(&thW2,0,W2,0);
pthread_join(thW1,0);
//pthread_join(thW2,0);
fclose(fd);
wait(0);
}
}
The output is as follows-
child created
W2Thread-1
R1Thread
R1Thread-1
W2Thread-1
W2Thread-1
W2Thread-1
W2Thread-1
W2Thread-1
W2Thread-1
W2Thread-1
W2Thread-1
The condition_wait in child never comes out.
There are potential issues with using multiple processes, each with multiple threads, but they mostly revolve around program state at the time of the fork. Since your program forks before it creates any additional threads, you can be confident about its state at that time, and in particular, you can be confident that its one thread is not at that time executing in a critical section. This is fine.
However, you are missing two key details:
Although you set the mutex to be process-shared, the version of the code you initially presented failed to do the same for the condition variable.
Setting pthread_* synchronization objects to be process-shared is necessary, but not sufficient, for inter-process use. For that, you need the synchronization objects to reside in shared memory accessed by all participating processes. Only that way can all the process access the same objects.
I implemented forked multiple clients in c and they are all supposed to write to a common file. This has failed so far since the information from these sockets is all messed up in the file destination.
This is my code
FILE *Ufptr;
Ufptr = fopen("Unsorted_busy_list.txt","a+");
fprintf(Ufptr, "%s:%d\n",inet_ntoa(newAddr.sin_addr),ntohs(newAddr.sin_port));
fclose(Ufptr);
I've been told that using fcntl and mutex lock onto the file could do, but am new to this and don't know how to implement this around the file writing process.
Any help
As I mentioned in a comment, if the parent consumes the output from the children, it is usually easier to use an Unix domain datagram socket pair (or pair per child process). Unix domain datagram sockets preserve message boundaries, so that each datagram successfully sent using send() is received in a single recv(). You can even send data as binary structures. No locks are needed. If you use a socket pair per child, you can easily set the parent side to nonblocking, and use select() or poll() to read datagrams from all in a single loop.
Back to the question proper.
Here is an example implementation of append_file(filename, format, ...) that uses POSIX.1-2008 vdprintf() to write to the file, using fcntl()-based advisory record locks:
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
int append_file(const char *filename, const char *format, ...)
{
struct flock lock;
va_list args;
int fd, cause;
/* Sanity checks. */
if (!filename || !*filename)
return errno = EINVAL;
/* Open the file for appending. Create if necessary. */
fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0666);
if (fd == -1)
return errno;
/* Lock the entire file exclusively.
Because we use record locks, append_file() to the same
file is NOT thread safe: whenever the first descriptor
is closed, all record locks to the same file in the process
are dropped. */
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if (fcntl(fd, F_SETLKW, &lock) == -1) {
cause = errno;
close(fd);
return errno = cause;
}
if (format && *format) {
cause = 0;
va_start(args, format);
if (vdprintf(fd, format, args) < 0)
cause = errno;
va_end(args);
if (cause) {
close(fd);
return errno = cause;
}
}
/* Note: This releases ALL record locks to this file
in this process! */
if (close(fd) == -1)
return errno;
/* Success! */
return 0;
}
int main(int argc, char *argv[])
{
int arg = 1;
if (argc < 3) {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
fprintf(stderr, " %s FILENAME STRING [ STRING ... ]\n", argv[0]);
fprintf(stderr, "\n");
}
for (arg = 2; arg < argc; arg++)
if (append_file(argv[1], "%s\n", argv[arg])) {
fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
If all writers use the above append_file() to append to the file, it is safe to rename the file at any point. (Just note that it is possible for one or more processes to do a final appending to the file after the rename, if they were waiting for the record lock to be released during the rename.)
To truncate the file, first take an exclusive lock on it, and then call ftruncate(fd, 0).
To read the file, take an fcntl()-based shared lock F_RDLCK (allowing other readers at the same time; or F_WRLCK, if you intend to "atomically" truncate the file after you've read the current contents), or you may see a partial final record at the end.
I am trying to understand POSIX file-region locks in C. The program below is really simple, sets the lock to F_WRLCK and then gets locks. There is no errors during opening/setting lock. Unfortunatelly it's always returning F_UNLCK. Where is the mistake ? Is it possible that it doesnt work on OSX correctly ?
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
void printLockType(int lock) {
if ( lock == F_RDLCK ) {
printf("readlock %i \n", lock);
}else if ( lock == F_WRLCK ) {
printf("writelock %i \n", lock);
}else if ( lock == F_UNLCK ) {
printf("unlock %i \n", lock);
} else {
printf("other %i\n", lock);
}
}
int main(int argc, char *argv[])
{
int fd;
struct flock fl ,fl2;
fl2.l_type = F_RDLCK; /* read/write lock */
fl2.l_whence = 0; /* beginning of file */
fl2.l_start = 0; /* offset from l_whence */
fl2.l_len = 100; /* length, 0 = to EOF */
fl2.l_pid = getpid();
fl.l_type = F_WRLCK; /* read/write lock */
fl.l_whence = 0; /* beginning of file */
fl.l_start = 0; /* offset from l_whence */
fl.l_len = 1000; /* length, 0 = to EOF */
fl.l_pid = getpid();
if ((fd = open("xxx", O_RDWR)) == -1) {
perror("open");
exit(1);
}
if (fcntl(fd, F_SETLK, &fl) == -1) {
perror("fcntl");
exit(1);
}
if(fcntl(fd, F_GETLK, &fl2) == -1) {
printf("%s \n", strerror(errno));
} else {
printLockType(fl2.l_type);
}
return 0;
}
You're misunderstanding the F_GETLK query. It returns F_UNLCK when nothing blocks the calling process from placing a lock of the given type at the given position.
Since the calling process is the one that created these existing locks, it can also create this new lock.
The Mac OS X manuals say
F_GETLK
Get the first lock that blocks the lock description pointed to by the third argument, arg,
taken as a pointer to a struct flock (see above). The information retrieved overwrites the
information passed to fcntl in the flock structure. If no lock is found that would prevent
this lock from being created, the structure is left unchanged by this function call except
for the lock type which is set to F_UNLCK.
I write a test for FIFO. Server writes string "hello" to the client through FIFO. But it seems that the two processes are blocked.I think the FIFO are opened for writing and reading by server and client. But the two processes output nothing.
/* FIFO test */
#include <stdio.h>
#include <sys/types.h>
#include <sys.stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define FIFOPATH "/home/hel/fifo" // define file path
int client(void);
int server(void);
int main(void)
{
pid_t pid;
/* create FIFO */
if (mkfifo(FIFOPATH, S_IRUSR | S_IWUSR) < 0) {
if (errno == EEXIST) { // already exists, ok
}
/* error */
else {
exit(-1);
}
}
/* create process */
pid = fork();
if (pid < 0) { // error, process exits.
exit(-1);
} else if (pid == 0) { // child, server
server();
return 0; // exit
}
/* parent, client */
client();
return 0;
}
/* server */
int server(void)
{
int ret;
int fd;
/* open fifo for writing */
if ((fd = open(FIFOPATH, 0200)) < 0) {
printf("%s\n", strerror(errno));
return -1; // error
}
ret = write(fd, "hello", 5);
close(fd);
return 0;
}
/* client */
int client(void)
{
char recvBuf[100];
int fd;
int ret;
/* open fifo for reading */
if ((fd = open(FIFOPATH, 0400)) < 0) {
printf("%s\n", strerror(errno));
return -1; // error
}
ret = read(fd, recvBuf, 5);
printf("ret: %d %d\n", ret, fd);
printf("client receive %s\n", recvBuf);
close(fd);
return 0;
}
Your code has two problems. The first one is the main problem.
The flags parameters passed to open are incorrect. They are not supposed to be unix file permission flags as it appears you have provided. The server should use O_WRONLY and the client should use O_RDONLY.
write(fd, "hello", 5); and read(fd, recvBuf, 5); are not writing and reading the terminating NUL character of the string. But then it is printed as a string: printf("client receive %s\n", recvBuf);. This invokes Undefined Behaviour (even though there is a good chance the program may appear to "work"). Change 5 to 6.
open() uses following flags:-
O_RDONLY open for reading only
O_WRONLY open for writing only
O_RDWR open for reading and writing
O_NONBLOCK do not block on open or for data to become available
O_APPEND append on each write
O_CREAT create file if it does not exist
O_TRUNC truncate size to 0
O_EXCL error if O_CREAT and the file exists
O_SHLOCK atomically obtain a shared lock
O_EXLOCK atomically obtain an exclusive lock
O_NOFOLLOW do not follow symlinks
O_SYMLINK allow open of symlinks
O_EVTONLY descriptor requested for event notifications only
O_CLOEXEC mark as close-on-exec
for FIFO you must use O_RDONLY in client and O_WRONLY in server in your program.
0200 and 0400 permissions are not working for open(). you can check the the flag value in as
#define O_RDONLY 0x0000 / open for reading only */
#define O_WRONLY 0x0001 / open for writing only */
thats why open blocks in your case as it doesn't get correct flag.
I implemented the Reader/Writer problem in C using semaphores (http://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem).
If I put the respective processes to sleep() for one second, the program acts as expected, however if I let it run without interrupting I get something like this:
writer wrote 5
writer wrote 10
reader reads the data:5
writer wrote 15
reader reads the data:15
reader reads the data:15
Semaphore with value of sid = 11599873 is killed
Semaphore with value of sid = 11632642 is killed
Semaphore with value of sid = 11665411 is killed
or
writer wrote 5
writer wrote 10
writer wrote 15
reader reads the data:5
reader reads the data:15
reader reads the data:15
Semaphore with value of sid = 11599873 is killed
Semaphore with value of sid = 11632642 is killed
Semaphore with value of sid = 11665411 is killed
Is this just because the standard out buffer prints these lines out of order, or is there a race condition in my implementation?
Here's the code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdlib.h>
#include "sem.h"
#define N 3
#define BUFSIZE 1
#define PERMS 0666 //0666 - To grant read and write permissions
int *buffer;
int mutex,write_allowed,rd_count; /* semaphore variables
* mutex, write_allowed - binary semaphore -- critical section
* rd_count - counting semaphore */
void reading()
{
//perform read
int g;
g=*buffer;
printf("reader reads the data:%d\n",g);
}
int main()
{
int shmid,no=1,i;
int pid,n;
if((shmid=shmget(1000,BUFSIZE,IPC_CREAT| PERMS )) < 0)
{
printf("\n unable to create shared memory");
return;
}
if((buffer=(int*)shmat(shmid,(char*)0,0)) == (int*)-1)
{
printf("\n Shared memory allocation error\n");
exit(1);
}
// semaphore creation
if((mutex=semget(IPC_PRIVATE,1,PERMS | IPC_CREAT)) == -1)
{
printf("\n can't create mutex semaphore");
exit(1);
}
if((write_allowed=semget(IPC_PRIVATE,1,PERMS | IPC_CREAT)) == -1)
{
printf("\n can't create empty semaphore");
exit(1);
}
if((rd_count=semget(IPC_PRIVATE,1,PERMS | IPC_CREAT)) == -1)
{
printf("\ncan't create full semaphore");
exit(1);
}
// initialze the semaphore
sem_create(mutex,1);
sem_create(write_allowed,1);
sem_create(rd_count,0);
//forking a child
if((pid=fork()) < 0)
{
printf("\n Error in process creation");
exit(1);
}
// write process
if(pid > 0)
{
for(i=0;i<N;i++)
{
P(write_allowed);
//perform write
*buffer=*buffer+5;
printf("write wrote %d\n", *buffer);
//sleep(1);
V(write_allowed);
}
}
//read process
if(pid == 0)
{
for(i=0;i<N;i++)
{
P(mutex);
rd_count++;
if(1 == rd_count)
P(write_allowed);
V(mutex);
reading();
//sleep(1);
P(mutex);
rd_count--;
if(0==rd_count)
V(write_allowed);
V(mutex);
}
shmdt(0);
shmctl(shmid, IPC_RMID, NULL);
semkill(mutex);
semkill(write_allowed);
semkill(rd_count);
}
}
The sem.h file:
/************************************************************************/
/* Operating Systems - Fall 2006
/* */
/* Semaphore library : sem.h */
/* */
/* Originally developed at KSU by a teaching assistant */
/* */
/* Description : The following library is a collection of */
/* routines for using binary semaphores in C: */
/* 1. seminit - to initialize a semaphore. */
/* 2. P - to perform a P(S) (wait) operation. */
/* 3. V - to perform a V(S) (signal) operation. */
/* 4. semkill - to remove a semaphore */
/* */
/* These routines call system routines: */
/* 1. semget - to get a semaphore */
/* 2. semctl - semaphore control operations */
/* 3. semop - semaphore operations */
/* */
/* Complete manual entries can be obtained by: */
/* man semctl | col -b | lpr */
/************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
union arg{ /* This structure is used to call semctl */
int val;
struct semid_ds *buf;
char *array;
};
/*
* Create semaphore based on "key" parameter to "initval"
*/
void sem_create(int semid, int initval)
{
int semval;
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
}s;
s.val=initval;
if((semval=semctl(semid,0,SETVAL,s))<0)
printf("\n Erroe in executing semctl");
}
/*
* Remove semaphore with semaphore id (sid) from the kernel
*/
static void semkill (sid)
int sid;
{
if (semctl(sid,0,IPC_RMID,0) == -1)
perror("semctl (kill)");
printf("Semaphore with value of sid = %d is killed \n",sid);
}
/*
* Perform the designated "op" operation on the semaphore. If "op" is -1,
* then this implements the "P" operation; it decrements the value of the
* semaphore (semval) if it was >0,
* and blocks the caller if it was zero (semval==0)
* If "op" is 1, then this is simply added to current value of
* the semaphore ("V" operation).
*/
static void semcall(sid, op)
int sid;
int op;
{
struct sembuf sb;
sb.sem_num = 0; /* semaphore number within sid */
sb.sem_op = op;
sb.sem_flg = 0; /* blocking call */
if (semop(sid, &sb, 1) == -1)
perror("semop");
}
/*
* P operation on semaphore "sid". Should be called upon entry to critical
* region.
*/
static void P(sid)
int sid;
{
semcall(sid, -1);
}
/*
* V operation on semaphore "sid". Should be called upon exit from critical
* region.
*/
static void V(sid)
int sid;
{
semcall(sid, 1);
}
You don't increment a semaphore by rd_count++;. rd_count is supposedly just a semaphore ID. You need to use some kind of function that operates using the semaphore ID to change the state of the semaphore, which is kept somewhere outside your two processes.
Also I am not familiar with sem_create(), but unless it is a C++ function taking a reference argument, I have my doubts it has any side-effect on the variables you pass it.