当前位置: 技术问答>linux和unix
关于进程的小问题,请指教!!!
来源: 互联网 发布时间:2015-05-22
本文导语: 问题是:client.c的子进程读出的nbytes=0,不知道为什么? 请各位高手指导!!!,谢谢了 代码如下: server.c #include "cliserv.h" typedef struct list { char name[20]; pid_t client_pid; struct list *next; }online_list; online_list *head;/*head...
问题是:client.c的子进程读出的nbytes=0,不知道为什么?
请各位高手指导!!!,谢谢了
代码如下:
server.c
#include "cliserv.h"
typedef struct list
{
char name[20];
pid_t client_pid;
struct list *next;
}online_list;
online_list *head;/*head里面没有存东西,从head->next开始*/
void addList(char name[],int pid)
{
online_list *p,*q;
p=head;
while(p->next!=NULL)
p=p->next;
q=(online_list *)malloc(sizeof(online_list));
sprintf(q->name,name);
q->client_pid=pid;
q->next=NULL;
p->next=q;
printf("add %sn",name);
}
delList(int pid)
{
online_list *p,*q;
p=head;
while (p->next!=NULL&&p->next->client_pid!=pid)
{
p=p->next;
}
p->next=p->next->next;
}
void writeAll(char message[])
{
online_list *p;
char client_fifo_name[256];
int client_fifo_fd=-1;
p=head->next;
/*********/
while (p!=NULL)
{
printf("client_pid=%dn",p->client_pid);
sprintf(client_fifo_name,CLIENT_FIFO_NAME,p->client_pid);
client_fifo_fd=open(client_fifo_name,O_WRONLY);
printf("client_fifo_fd=%dn",client_fifo_fd);
if(client_fifo_fd!=-1)
{
write(client_fifo_fd,&message,sizeof(message));
printf("write to the client message=%s",message);
close(client_fifo_fd);
}
p=p->next;
}
/****************/
}
int main(int argc,char* argv[])
{
int server_fifo_fd,client_fifo_fd;
struct data_to_pass client_data;
int nbytes;
char *tmp_char_ptr;
char message[256];
int client_pid=-1;
int count=0;
/*init*/
mkfifo(SERVER_FIFO_NAME,0777);
server_fifo_fd=open(SERVER_FIFO_NAME,O_RDONLY);
if(server_fifo_fd==-1)
err_exit("Server fifo failure");
head=(online_list *)malloc(sizeof(online_list));
head->client_pid=-1;
sprintf(head->name,"server");
head->next=NULL;
/*sleep(10);*/
do{
nbytes=read(server_fifo_fd,&client_data,sizeof(client_data));
client_pid=client_data.client_pid;
if(nbytes==0)
continue;
if(client_pid==-1)
err_exit("client_pid error!!n");
if(strcmp(client_data.message,"COME IN.n")==0)
{
addList(client_data.name,client_data.client_pid);
sprintf(message,"system hint:%s come in!!!n",client_data.name);/*maybe error*/
}
else if (strcmp(client_data.message,"EXITn")==0)
{
delList(client_data.client_pid);
sprintf(message,"system hint:%s leave!!!n",client_data.name);/*maybe error*/
}
else
{
sprintf(message,"%s:%s",client_data.name,client_data.message);/*maybe error*/
}
writeAll(message);
printf("%s:%s",client_data.name,client_data.message);
}
while(head->next!=NULL);/*检查是否list为空*/
close(server_fifo_fd);
unlink(SERVER_FIFO_NAME);
return 0;
}
client.c
#include "cliserv.h"
void readAll(int client_fifo_fd)
{
char message[256];
int nbytes;
nbytes=read(client_fifo_fd,&message,sizeof(message));
if(nbytes>0)
{
printf("%s",message);
}
/*printf("nbytes=%dn",nbytes);*/
}
int main()
{
int server_fifo_fd,client_fifo_fd;
struct data_to_pass my_send;
char client_fifo_name[256];
pid_t mypid,sonpid;
char command[256];
int len;
server_fifo_fd=open(SERVER_FIFO_NAME,O_WRONLY);
if(server_fifo_fd==-1)
err_exit("sorry,no server");
mypid=getpid();
printf("if you want end the chat,input the 'EXIT'n");
printf("please input your name:");
scanf("%s",my_send.name);
sprintf(my_send.message,"COME IN.n");
if((sonpid=fork())
请各位高手指导!!!,谢谢了
代码如下:
server.c
#include "cliserv.h"
typedef struct list
{
char name[20];
pid_t client_pid;
struct list *next;
}online_list;
online_list *head;/*head里面没有存东西,从head->next开始*/
void addList(char name[],int pid)
{
online_list *p,*q;
p=head;
while(p->next!=NULL)
p=p->next;
q=(online_list *)malloc(sizeof(online_list));
sprintf(q->name,name);
q->client_pid=pid;
q->next=NULL;
p->next=q;
printf("add %sn",name);
}
delList(int pid)
{
online_list *p,*q;
p=head;
while (p->next!=NULL&&p->next->client_pid!=pid)
{
p=p->next;
}
p->next=p->next->next;
}
void writeAll(char message[])
{
online_list *p;
char client_fifo_name[256];
int client_fifo_fd=-1;
p=head->next;
/*********/
while (p!=NULL)
{
printf("client_pid=%dn",p->client_pid);
sprintf(client_fifo_name,CLIENT_FIFO_NAME,p->client_pid);
client_fifo_fd=open(client_fifo_name,O_WRONLY);
printf("client_fifo_fd=%dn",client_fifo_fd);
if(client_fifo_fd!=-1)
{
write(client_fifo_fd,&message,sizeof(message));
printf("write to the client message=%s",message);
close(client_fifo_fd);
}
p=p->next;
}
/****************/
}
int main(int argc,char* argv[])
{
int server_fifo_fd,client_fifo_fd;
struct data_to_pass client_data;
int nbytes;
char *tmp_char_ptr;
char message[256];
int client_pid=-1;
int count=0;
/*init*/
mkfifo(SERVER_FIFO_NAME,0777);
server_fifo_fd=open(SERVER_FIFO_NAME,O_RDONLY);
if(server_fifo_fd==-1)
err_exit("Server fifo failure");
head=(online_list *)malloc(sizeof(online_list));
head->client_pid=-1;
sprintf(head->name,"server");
head->next=NULL;
/*sleep(10);*/
do{
nbytes=read(server_fifo_fd,&client_data,sizeof(client_data));
client_pid=client_data.client_pid;
if(nbytes==0)
continue;
if(client_pid==-1)
err_exit("client_pid error!!n");
if(strcmp(client_data.message,"COME IN.n")==0)
{
addList(client_data.name,client_data.client_pid);
sprintf(message,"system hint:%s come in!!!n",client_data.name);/*maybe error*/
}
else if (strcmp(client_data.message,"EXITn")==0)
{
delList(client_data.client_pid);
sprintf(message,"system hint:%s leave!!!n",client_data.name);/*maybe error*/
}
else
{
sprintf(message,"%s:%s",client_data.name,client_data.message);/*maybe error*/
}
writeAll(message);
printf("%s:%s",client_data.name,client_data.message);
}
while(head->next!=NULL);/*检查是否list为空*/
close(server_fifo_fd);
unlink(SERVER_FIFO_NAME);
return 0;
}
client.c
#include "cliserv.h"
void readAll(int client_fifo_fd)
{
char message[256];
int nbytes;
nbytes=read(client_fifo_fd,&message,sizeof(message));
if(nbytes>0)
{
printf("%s",message);
}
/*printf("nbytes=%dn",nbytes);*/
}
int main()
{
int server_fifo_fd,client_fifo_fd;
struct data_to_pass my_send;
char client_fifo_name[256];
pid_t mypid,sonpid;
char command[256];
int len;
server_fifo_fd=open(SERVER_FIFO_NAME,O_WRONLY);
if(server_fifo_fd==-1)
err_exit("sorry,no server");
mypid=getpid();
printf("if you want end the chat,input the 'EXIT'n");
printf("please input your name:");
scanf("%s",my_send.name);
sprintf(my_send.message,"COME IN.n");
if((sonpid=fork())