세마포어 관련 질문인데요
글쓴이: limasdf / 작성시간: 목, 2004/11/11 - 12:35오전
소스보면
공유메모리에다가 자식프로세스,부모프로세스에서 둘다 값을 증가시키고.
공유메모리부분을 세마포어로 했는데요
결과가
child : the result is 100000000
parent : the result is 200000000
이렇게 나오길 바라는데
왜! parent만 200000000이 나오고 자식은 10000000이 안나오는거죠-_-
;;
사실 숙제긴하지만 요거땜에 무지 고민도 해보고; 책도 찾아ㅤㅂㅘㅅ는데 모르겠네요;
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/sem.h>
int main()
{
struct sembuf psembuf={0,-1,SEM_UNDO};
struct sembuf vsembuf={0,1,SEM_UNDO};
int semId;
int smId; //shared memory id
int *s_memory = (int *)0; // declare variable for shared memory
int i,j; // for-loop
semId = semget(75,1,0777|IPC_CREAT);
semctl(semId,0,SETVAL,1);
smId = shmget((key_t)75,4,0777|IPC_CREAT);
if((fork())==0)
{
s_memory=(int *)shmat(smId,0,0);
semop(semId,&psembuf,1);
for(i=0;i<10000;i++)
for(j=0;j<10000;j++)
*s_memory = *s_memory+1;
semop(semId,&vsembuf,1);
printf("child : the result is %d\n",*s_memory);
}
else
{
s_memory=(int *)shmat(smId,0,0);
semop(semId,&psembuf,1);
for(j=0;j<10000;j++)
for(i=0;i<10000;i++)
*s_memory = *s_memory+1;
semop(semId,&vsembuf,1);
printf("parent: the result is %d\n",*s_memory);
}
return 0;
}
Quote:
결과child : the result is 125590800
parent: the result is 200000000
Forums:


printf 후에 v op 를 해보세요.
printf 후에 v op 를 해보세요.
---------
간디가 말한 우리를 파괴시키는 7가지 요소
첫째, 노동 없는 부(富)/둘째, 양심 없는 쾌락
셋째, 인격 없는 지! 식/넷째, 윤리 없는 비지니스
이익추구를 위해서라면..
다섯째, 인성(人性)없는 과학
여섯째, 희생 없는 종교/일곱째, 신념 없는 정치
감사합니다 ㅠㅠ제가 초보라서 엉엉,. 문제는 critical se
감사합니다 ㅠㅠ
제가 초보라서 엉엉,.
문제는 critical section을 *y=*y+1 를 제외하고 생각했네요
감사합니다
댓글 달기