锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
#include "semaphore.h"
#include "pthread.h"
#define N 5
static int necs;
static sem_t *forks;
void *
takeFork(int i)
{
if(i==N-1)
{
sem_wait(&forks[0]);
sem_wait(&forks[i]);
}
else
{
sem_wait(&forks[i]);
sem_wait(&forks[i+1]);
}
}
void *
putFork(int i)
{
if(i==N-1)
{
sem_post(&forks[0]);
sem_post(&forks[i]);
}
else
{
sem_post(&forks[i]);
sem_post(&forks[i+1]);
}
}
void
thinking(int i,int necs)
{
printf("pholosopher %d is thinking\n",i);
sleep(necs);
}
void
eating(int i,int necs)
{
printf("pholosopher %d is eating\n",i);
sleep(necs);
}
void *
philosopher(void *n)
{
int i=(int )n;
while(1)
{
thinking(i,necs);
takeFork(i);
eating(i,necs);
putFork(i);
}
}
//============================main function
int
main(int argc,char *argv[])
{
pthread_t tid;
if(argc==1)
necs=2;
else if(argc ==2)
necs=atoi(argv[1]);
else return 1;
forks=(sem_t*)malloc(N*sizeof(sem_t));
int i;
for(i=0; i<N; i++)
sem_init(forks+i,0,1);
int status;
for(i=0;i<N;i++)
{
status=pthread_create(&tid,NULL,philosopher,(void*)i);
if(status<0)
err_sys("create error!");
}
pthread_join(tid ,NULL);
return 0;
}
]]>
#include "lock.h"
#include "lock.c"
/*define some important variable*/
/*=======================================*/
pid_t pid;
static char *forks[5]={
"fork0", "fork1", "fork2" ,"fork3" ,"fork4"
};
static int necs;
#define N 5
/*======================================*/
void takeFork(int i)
{
if(i==N-1)
{
lock(forks[0]);
lock(forks[i]);
}
else
{
lock(forks[i]);
lock(forks[i+1]);
}
}
void putFork(int i)
{
if(i==N-1)
{
unlock(forks[0]);
unlock(forks[i]);
}
else
{
unlock(forks[i]);
unlock(forks[i+1]);
}
}
int
thinking(int i,int necs){
printf("pholosopher %d is thinking\n",i);
return sleep(necs);
}
int
eating(int i,int necs)
{printf("pholosopher %d is eating\n",i);
return sleep(necs);
}
void philosopher(int i)
{
while(1)
{
thinking(i,necs);
takeFork(i);
eating(i,necs);
putFork(i);
}
}
int
main(int argc ,char *argv[])
{
if(argc=1)
necs=2;
else if(argc==2)
necs=atoi(argv[1]);
else return 0;
int i;
for(i=0;i<N;i++)
{
pid=fork();
if(pid ==0)
philosopher(i);
}
return 0;
}
]]>
07.11.20 浜庢搗闊?br>*/
#include "sys/times.h"
#include "apue.h"
#include "fcntl.h"
clock_t times(struct tms *buf);
static void pr_times(clock_t real ,struct tms *tmsstart, struct tms *tmsend);
int main(int argc, char *argv[])
{
struct tms tmsstart,tmsend;
clock_t start,end;
off_t excursion;
if((excursion=lseek(0,0,SEEK_END))==-1)printf("can not seek!\n");
char buf[excursion];
/*printf("excutsion ==#%d\n",excursion);*/
int outfile_fd;/*out file descrite tag */
lseek(0,0,SEEK_SET);/*it is very importent to go back to the beginning of input file!*/
if(read(0,buf,excursion)==0)printf("read OK!\n");
/* buf[excursion]='\0';*/
if(argc==3 && strcmp(argv[2],"sync")==0)
{ if(( outfile_fd=open(argv[1],O_RDWR|O_CREAT|O_SYNC,0666))<0)err_sys("creat error");}
else if(argc==2)
{if (( outfile_fd=open(argv[1],O_RDWR|O_CREAT,0666))<0)err_sys("creat error");}
else{printf("para error!\n");exit(1);}
long size=1024;
fflush(stdin);
printf("BUFFSIZE USER CPU SYSTEM CPU REAL TIMES CIRCLE TIMES\n");
int n;
int t;
char *p=buf;
long redo_count;/*re do copy times*/
do{
if((start=times(&tmsstart))==-1)err_sys("times error");
redo_count=0;/*it is very import to set the counter zero at every circle */
t=1;/*it is use in the inside while */
while((n=write(outfile_fd,p,size))==size)
{ p+=size;/*the begin buf must increase every time*/
redo_count++;/*count how many times circle do*/
t=size*redo_count;/*one tag to count how many charaters we have travered*/
/* printf("excursion==%d t==%d excursion-t==%d size==%d\n",excursion,t,excursion-t,size);*/
if((excursion-t)<size)break;
}
if(excursion-t>0)
if(write(outfile_fd,p,excursion-t)!=(excursion-t))err_sys("read error");
if((end=times(&tmsend))==-1)err_sys("times error");
printf("%7ld",size);
pr_times(end-start,&tmsstart,&tmsend);
printf("%7ld\n",redo_count);
lseek(outfile_fd,0,SEEK_SET);/*let the outfile pointer go back to the beginning of outfile!*/
p=buf;/*let p return to the beginning of buf;*/
/* printf("size=%d excursion=%d\n",size,excursion);*/
} while((size*=2)<=excursion);
return 0;
}
static void
pr_times(clock_t real ,struct tms *tmsstart,struct tms *tmsend)
{static long clktck=0;
if(clktck==0)
if((clktck=sysconf(_SC_CLK_TCK))<0)
printf("sysconf error");
printf(" %2.5f",(tmsend->tms_utime-tmsstart->tms_utime)/(double)clktck);
printf(" %2.5f",(tmsend->tms_stime-tmsstart->tms_stime)/(double)clktck);
printf(" %2.5f ",real/(double)clktck);
}
]]>#include "apue.h"
#include <sys/wait.h>
extern char **environ;
static void sign_Int(int signnal)
{printf("Be interrupted\n%%");}
static void sign_Quit(int signnal)
{printf("quit\n%%");
}
int
main(void)
{
char *argv[20];
pid_t pid;
int status;
if(signal(SIGINT,sign_Int)==SIG_ERR)
err_sys("signal error\n");
if(signal(SIGQUIT,sign_Quit)==SIG_ERR)
err_sys("signal error\n");
printf("%%");
while (1)
{ char buf[MAXLINE];
int index=0,i=0;
if(fgets(buf,MAXLINE,stdin)==NULL)
break;
while( buf[i] != '\0'&&buf[i]!='\n' )
{
if(buf[i] != ' ' )
{
argv[index]= buf+i;
index++;
for(;buf[i]!= ' '&&buf[i]!='\n';i++);
if(buf[i]=='\n')
break;
buf[i]='\0';
i++;
while(buf[i]==' ')
i++;
}
}
buf[i]='\0';
argv[index] = NULL;
if((pid = fork() ) < 0 )
err_sys("fork error\n");
else if( pid == 0 )
{
execve(argv[0],argv,environ);
err_ret("coundn't execute:%s",argv[0]);
exit(127);
}
if(( pid = waitpid(pid,&status,0)) < 0)
err_sys("waitpid error\n");
printf("%%");
}
exit(0);
}
]]>