Mutual lock Problem

Hi guys.I am working on a problem where I have 3 processes and they communicate with shared memory.I am also using semaphores for mutual exclusion.First process writes first 512 bytes to shared memory buffer that are random letters,second process wirtes last 512 bytes to shared memory buffer that are random numbers and the third reads that date from shared memory and writes to a file>I cab't see where they lock each other.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#define PRVI_KEY 10001
#define DRUGI_KEY 10002
#define TRECI_KEY 10003
#define MEM_KEY 10004
#define N 1024
union semun{
int val;
struct semid_ds *buf;
ushort *array;
struct seminfo * __buf;
void * __pad;
};
int main()
{
int sem1,sem2,sem3,memid;
int i,j;
FILE *f;
char * shmem;
char buff[N+1];
struct sembuf lock={0,-1,0};
struct sembuf unlock={0,1,0};
union semun opts;
sem1=semget(PRVI_KEY,1,IPC_CREAT | 0777);
sem2=semget(DRUGI_KEY,1,IPC_CREAT | 0777);
sem3=semget(TRECI_KEY,1,IPC_CREAT | 0777);
opts.val=1;
semctl(sem1,0,SETVAL,opts);
opts.val=0;
semctl(sem2,0,SETVAL,opts);
opts.val=0;
semctl(sem3,0,SETVAL,opts);
memid=shmget(MEM_KEY,N*sizeof(char),IPC_CREAT | 0777);
if(fork()==0)
{
shmem=(char *)shmat(memid,NULL,0);
for(i=0;i<10;i++)
{
semop(sem1,&lock,1);
for(j=0;j<N/2;j++)
shmem[j]='a'+rand()%('z'-'a'+1);
semop(sem2,&unlock,1);
}
shmdt(shmem);
return 0;

}
if(fork()==0)
{
shmem=(char *)shmat(memid,NULL,0);
for(i=0;i<10;i++)

{
semop(sem2,&lock,1);
for(j=N/2;j<N;j++)
shmem[j]='0'+rand()%('9'-'0'+1);
semop(sem3,&unlock,1);


}
shmdt(shmem);
return 0;

}
shmem=(char *)shmat(memid,NULL,SHM_RDONLY);
for(i<0;i<10;i++)
{
semop(sem3,&lock,1);

char buff[N+1];
strncpy(buff,shmem,N);

buff[N]='\0';

f=fopen("sadrzaj","w");
fprintf(f,"%s\n",buff);
fclose(f);
semop(sem1,&unlock,1);



}
shmdt(shmem);
semctl(sem1,0,IPC_RMID,opts);
semctl(sem2,0,IPC_RMID,opts);
semctl(sem3,0,IPC_RMID,opts);
shmctl(memid,IPC_RMID,0);
return 0;


}
Last edited on
Take a look at line 75: You lock sem3 while unlock sem1
But does not then process 3(locked by sem3) become locked and process 1(locked by sem1) becomes unlocked again?Sorry,but i can't see the mistake.
> Sorry,but i can't see the mistake.
Is your code as badly formatted as the stuff you've given us to read.

But does not then process 3(locked by sem3) become locked and process 1(locked by sem1) becomes unlocked again?
No, the locking/unlocking applies only for the current thread.

When thread_a locks sem3 and thread_b later trys to lock sem3 thread_b is blocked until thread_a unlocks sem3.

That's the point: The the shared resource is protected against simultaneous access.

Since you have only one resource to protecte (shmem) you should have only one sem.
@coder777
Thank you very much for the explanation!
Topic archived. No new replies allowed.