linux多线程问题

留影 2011-06-20 06:38:33
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>


#define PROCMAXLEN 11//进程名最大长度

typedef struct pcb
{
char name[PROCMAXLEN];
int time;//预计执行时间
short priority;//进程当前优先级
struct pcb *next;//指向下一个PCB
}PCB,*pPCB;

void *create(void *head);
void *proc();

pPCB head=NULL;

/********************************************************************
数据初始化
进程队列的初始化
********************************************************************/
pPCB procline1;
pPCB procline2;
pPCB procline3;
pPCB rear1;
pPCB rear2;
pPCB rear3;


sem_t bin_sem;//信号控制cpu执行和等待

int main(int argc,char *argv[])
{
head=(pPCB)malloc(sizeof(PCB));
/********************************************************************
数据初始化
进程队列的初始化
********************************************************************/
procline1=(pPCB)malloc(sizeof(PCB));
procline1->next=NULL;
procline2=(pPCB)malloc(sizeof(PCB));
procline2->next=NULL;
procline3=(pPCB)malloc(sizeof(PCB));
procline3->next=NULL;

rear1=procline1;
rear2=procline2;
rear3=procline3;
//变量定义
pPCB temp=NULL,p=head;
char c;
int i=0;

/************************************************
共享数据初始化
进程的初始化
************************************************/
//判断是否输入进程序列
if(argc < 2)
{
fprintf(stderr,"没有输入进程序列文件!\n");
exit(-1);
}
//读取文件
FILE *file=fopen(argv[1],"r");
if(file == NULL)
{
fprintf(stderr,"文件打开失败!\n");
exit(-1);
}
//建立进程PCB,并初始化
char num[10]={0};
int j=0;
do
{
temp=(pPCB)malloc(sizeof(PCB));
memset(temp->name,0,PROCMAXLEN);
temp->priority=0;
temp->time=0;
temp->next=NULL;
i=0;
//填充进程名
while((c=(char)fgetc(file)) != '\n' && c != EOF)
{
temp->name[i++]=c;
if(i > 10)
{
fprintf(stderr,"进程名超过限制,进程名将被截断!");
while(fgetc(file) != '\n')
{
;
}
}
}
temp->name[i]='\0';

//填充进程预计时间
i=0;
while((c=(char)fgetc(file)) != '\n' && c!= EOF)
{
num[i++]=c;
}
for(j=0;j<i;j++)
{
temp->time*=10;
temp->time+=(int)num[j]-48;
}
//将进程插入队列
p->next=temp;
p=temp;
}while(fgetc(file) == '\n');//务必保证最后一个进程信息之后没有换行

/********************************************************************
create线程创建
*********************************************************************/
pthread_t creat_p;
int res;
void *thread_result;
res=pthread_create(&creat_p,NULL,create,(void *)head);
if(res != 0)
{
fprintf(stderr,"thread create error\n");
exit(-1);
}

/********************************************************************
proc线程创建
*********************************************************************/
pthread_t proc_p;
void *thread_result_proc;
res=pthread_create(&proc_p,NULL,proc,(void *)head);
if(res != 0)
{
fprintf(stderr,"thread create error\n");
exit(-1);
}


res=pthread_join(proc_p,&thread_result_proc);
if(res != 0)
{
fprintf(stderr,"thread join error\n");
exit(-1);
}
printf("%s\n",(char *)thread_result_proc);

res=pthread_join(creat_p,&thread_result);
if(res != 0)
{
fprintf(stderr,"thread join error\n");
exit(-1);
}
printf("%s\n",(char *)thread_result);
}

void *create(void *head)
{
int res=sem_init(&bin_sem,0,0);
if(res != 0)
{
fprintf(stderr,"semaphore initialization failed!");
exit(-1);
}


pPCB temp;
temp=((pPCB)head)->next;

while(temp != NULL)
{
rear1->next=temp;
rear1=rear1->next;
temp=temp->next;
rear1->next=NULL;
sem_post(&bin_sem);//增加信号量
printf("进程%s进入就绪队列!\n",rear1->name);
sleep(1);//每次一个任务间隔1秒
}
pthread_exit("-------------------所有进程进入就绪队列完成!------------------------\n");
}



void *proc()
{
int i=0;pPCB now=NULL;
int cpu=0;int status=0;//记录当前时候是可抢占状态
while(1)
{
sem_wait(&bin_sem);//信号量记录目前有多少进程等待cpu
//从队列中读取
sleep(2);
if(procline1->next != NULL)
{
now=procline1->next;
procline1->next=now->next;
}
else if(procline2->next != NULL)
{
now=procline2->next;
procline2->next=now->next;
status=1;
}
else if(procline3->next != NULL)
{
now=procline3->next;
procline3->next=now->next;
status=1;
}

//处理程序
switch(now->priority)
{
case 0:
cpu=2;break;
case 1:
cpu=4;break;
case 2:
cpu=8;break;
}

for(i=0;i<cpu;i++)
{

if(now->time >= 1)
now->time--;
else
{
break;
}
}
if(now->time == 0)
{
printf("进程%s执行结束!\n",now->name);
}
else
{
if(++(now->priority) == 1)
{
rear2->next=now;
now->next=NULL;
}
else
{
rear3->next=now;
now->next=NULL;
}
sem_post(&bin_sem);
}
}
}

当没有sleep(2)的时候输出不正常:
进程sun进入就绪队列!
进程sun执行结束!
进程zhang进入就绪队列!
进程sun执行结束!
进程ddfa进入就绪队列!
进程sun执行结束!

加上sleep(2)的时候就正常了:
进程sun进入就绪队列!
进程zhang进入就绪队列!
进程sun执行结束!
进程ddfa进入就绪队列!
进程ddfa执行结束!
进程zhang执行结束!

...全文
84 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
awsqsh 2011-06-21
  • 打赏
  • 举报
回复
sleep(2) 应该是在等待另外一个进程的执行完毕
开始领悟 2011-06-20
  • 打赏
  • 举报
回复
学习了,呵呵
cai99 2011-06-20
  • 打赏
  • 举报
回复
太长了,顶一下

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧