当前位置: 技术问答>linux和unix
为什么Segmentation fault呢?
来源: 互联网 发布时间:2015-05-21
本文导语: 这个程序老是报错,说“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 m...
这个程序老是报错,说“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 = %dn",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 %sn",*tmp_char_ptr);
while(*tmp_char_ptr)
{
*tmp_char_ptr=toupper(*tmp_char_ptr);
printf("ok:recive %sn",*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 = %dn",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_send0)
{
printf("%d received:%sn",mypid,my_request.text_data);
}
close(client_fifo_fd);
}
}
close(server_fifo_fd);
unlink(client_fifo_name);/*注意*/
exit(EXIT_SUCCESS);
}
clserv.h
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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
#include
#define err_exit(MESSAGE)(perror(MESSAGE),exit(1))
#include
pid_t getpid(void);
pid_t getppid(void);
#include
pid_t fork(void);
代码如下:
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 = %dn",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 %sn",*tmp_char_ptr);
while(*tmp_char_ptr)
{
*tmp_char_ptr=toupper(*tmp_char_ptr);
printf("ok:recive %sn",*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 = %dn",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_send0)
{
printf("%d received:%sn",mypid,my_request.text_data);
}
close(client_fifo_fd);
}
}
close(server_fifo_fd);
unlink(client_fifo_name);/*注意*/
exit(EXIT_SUCCESS);
}
clserv.h
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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
#include
#define err_exit(MESSAGE)(perror(MESSAGE),exit(1))
#include
pid_t getpid(void);
pid_t getppid(void);
#include
pid_t fork(void);
|
char *tmp_char_ptr;
printf("read %sn",*tmp_char_ptr);
这里打印的是什么?
应该是只打印一个字符吧?--printf("read [%s]n", tmp_char_ptr);--不要*号
sprintf(client_fifo_name,CLIENT_FIFO_NAME,my_data.client_pid);
这里编译的时候没有报错么?
建议:
#include
编译的时候加 -Wall选项
一个makefile例子:
all : server
server : server.o
gcc -o server server.o (前面是Tab键,不是空格)
server.o: server.c
gcc -c -g -Wall server.c
printf("read %sn",*tmp_char_ptr);
这里打印的是什么?
应该是只打印一个字符吧?--printf("read [%s]n", tmp_char_ptr);--不要*号
sprintf(client_fifo_name,CLIENT_FIFO_NAME,my_data.client_pid);
这里编译的时候没有报错么?
建议:
#include
编译的时候加 -Wall选项
一个makefile例子:
all : server
server : server.o
gcc -o server server.o (前面是Tab键,不是空格)
server.o: server.c
gcc -c -g -Wall server.c