为什么Segmentation fault呢?
这个程序老是报错,说“Segmentation fault”;知道是server.c的18行错了,但不会改啊,请各位大哥指教指教~~~
代码如下:
server.c
#include "cliserv.h"
int main(int argc,char* argv[])
{
int server_fifo_fd,client_fifo_fd;
struct data_to_pass my_data;
int nbytes;
char client_fifo_name[256];
char *tmp_char_ptr;
mkfifo(SERVER_FIFO_NAME,0777);
server_fifo_fd=open(SERVER_FIFO_NAME,O_RDONLY);
if(server_fifo_fd==-1)
err_exit("Server fifo failure");
printf("server_fifo_fd = %d\n",server_fifo_fd);
/*sleep(10);*/
do{
nbytes=read(server_fifo_fd,&my_data,sizeof(my_data));
printf("read %d bytes",nbytes);
if(nbytes>0)
{
tmp_char_ptr=my_data.text_data;
printf("read %s\n",*tmp_char_ptr);
while(*tmp_char_ptr)
{
*tmp_char_ptr=toupper(*tmp_char_ptr);
printf("ok:recive %s\n",*tmp_char_ptr);
tmp_char_ptr++;
}
sprintf(client_fifo_name,CLIENT_FIFO_NAME,my_data.client_pid);
client_fifo_fd=open(client_fifo_name,O_WRONLY);
if(client_fifo_fd!=-1)
{
write(client_fifo_fd,&my_data,sizeof(my_data));
close(client_fifo_fd);
}
}
}
while(nbytes>0);
close(server_fifo_fd);
unlink(SERVER_FIFO_NAME);
return 0;
}
client.c
#include "cliserv.h"
int main()
{
int server_fifo_fd,client_fifo_fd;
struct data_to_pass my_request;
int times_to_send;
char client_fifo_name[256];
pid_t mypid;
server_fifo_fd=open(SERVER_FIFO_NAME,O_WRONLY);
printf("the server_fifo_fd = %d\n",server_fifo_fd);
if(server_fifo_fd==-1)
err_exit("sorry,no server");
mypid=getpid();
sprintf(client_fifo_name,CLIENT_FIFO_NAME,mypid);
if (mkfifo(client_fifo_name,0777)==-1)
{
err_exit(client_fifo_name);
}
for(times_to_send=0;times_to_send<5;times_to_send++)
{
sprintf(my_request.text_data,"hello from %d,%d",mypid,times_to_send);
my_request.client_pid=mypid;
printf("%d sent:%s.\n",mypid,my_request.text_data);
write(server_fifo_fd,&my_request,sizeof(my_request));
client_fifo_fd=open(client_fifo_name,O_RDONLY);
printf("the client_fifo_fd = %d\n",client_fifo_fd);
if(client_fifo_fd!=-1)
{
if(read(client_fifo_fd,&my_request,sizeof(my_request))>0)
{
printf("%d received:%s\n",mypid,my_request.text_data);
}
close(client_fifo_fd);
}
}
close(server_fifo_fd);
unlink(client_fifo_name);/*注意*/
exit(EXIT_SUCCESS);
}
clserv.h
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include "err_exit.h"
#define SERVER_FIFO_NAME "/tmp/serv_fifo"
#define CLIENT_FIFO_NAME "/tmp/client_%d_fifo"
#define BUFFER_SIZE 80
struct data_to_pass
{
pid_t client_pid;
char text_data[BUFFER_SIZE-1];
};
err_exit.h
#include <errno.h>
#include <stdlib.h>
#define err_exit(MESSAGE)(perror(MESSAGE),exit(1))
#include <unistd.h>
pid_t getpid(void);
pid_t getppid(void);
#include <unistd.h>
pid_t fork(void);