#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
#define PROCUNM 20
#define FNAME "/tmp/out"
#define LINESIZE 1024
static void P() ;
static void V();
static int semid;
static void P()
{
struct sembuf op;
op.sem_num=0;
op.sem_op=-1;
op.sem_flg=0;
while(semop(semid,&op,1)<0)
{
if(errno!=EINTR||errno!=EAGAIN)
{
perror("semop()");
exit(1);
}
}
}
static void V()
{
struct sembuf op;
op.sem_num=0;
op.sem_op=1;
op.sem_flg=0;
semop(semid,&op,1);
}
static void func_add(void)
{
FILE *fp;
int fd;
char linebuf[LINESIZE];
fp=fopen(FNAME,"r+");
if(fp==NULL)
{
perror("fopen()");
exit(1);
}
// fd=fileno(fp);
P();
fgets(linebuf,LINESIZE,fp);
fseek(fp,0,SEEK_SET);
sleep(1);
fprintf(fp,"%d\n",atoi(linebuf)+1);
fflush(fp);
V();
fclose(fp);
return ;
}
int main()
{
pid_t pid;
int i;
int semid;
//key=ftok();
semid= semget(IPC_PRIVATE,1,0600);//匿名ipc
if(semid<0)
{
perror("semget()");
exit(1);
}
if(semctl(semid,0,SETVAL,1)<0)
{
perror("semctl()");
exit(1);
}
for(i=0;i<PROCUNM;i++)
{
pid=fork();
if(pid<0)
{
perror("fork()");
exit(1);
}
if(pid==0)
{
func_add();
exit(0);
}
}
for(i=0;i<PROCUNM;i++)
wait(NULL);
semctl(semid,0,IPC_RMID);
exit(0);
}
//20个进程打开同一个文件+1操作,再写回去.
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
#define PROCUNM 20
#define FNAME "/tmp/out"
#define LINESIZE 1024
static void P() ;
static void V();
static int semid;
static void P()
{
struct sembuf op;
op.sem_num=0;
op.sem_op=-1;
op.sem_flg=0;
while(semop(semid,&op,1)<0)
{
if(errno!=EINTR||errno!=EAGAIN)
{
perror("semop()");
exit(1);
}
}
}
static void V()
{
struct sembuf op;
op.sem_num=0;
op.sem_op=1;
op.sem_flg=0;
semop(semid,&op,1);
}
static void func_add(void)
{
FILE *fp;
int fd;
char linebuf[LINESIZE];
fp=fopen(FNAME,"r+");
if(fp==NULL)
{
perror("fopen()");
exit(1);
}
// fd=fileno(fp);
P();
fgets(linebuf,LINESIZE,fp);
fseek(fp,0,SEEK_SET);
sleep(1);
fprintf(fp,"%d\n",atoi(linebuf)+1);
fflush(fp);
V();
fclose(fp);
return ;
}
int main()
{
pid_t pid;
int i;
int semid;
//key=ftok();
semid= semget(IPC_PRIVATE,1,0600);//匿名ipc
if(semid<0)
{
perror("semget()");
exit(1);
}
if(semctl(semid,0,SETVAL,1)<0)
{
perror("semctl()");
exit(1);
}
for(i=0;i<PROCUNM;i++)
{
pid=fork();
if(pid<0)
{
perror("fork()");
exit(1);
}
if(pid==0)
{
func_add();
exit(0);
}
}
for(i=0;i<PROCUNM;i++)
wait(NULL);
semctl(semid,0,IPC_RMID);
exit(0);
}
//20个进程打开同一个文件+1操作,再写回去.