编辑:现在我知道错误在主函数中 如果我删除它:
*A=0;
*NH=0;
*NS=0;
*in_boat=0;
然后我运行它没有段错误,为什么会发生这种情况?
以下代码返回分段错误11..i我不知道原因:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <time.h>
//--------------------Function declarations
void hacker_pier(int x);
void serf_pier(int x);
//--------------------Global variables
sem_t *sem_var, *sem_entry_pier, *sem_start_row, *sem_board_h, *sem_board_s, *sem_row_finished, *sem_file;//Semaphores
int *A,*I,*NH,*NS,*in_boat;//Count variables
int H,S,R,P;//User input variables
FILE *fp;//File pointer
int main(int argc, char *argv[])
{
int pid, pid2, pid3, pid4;//IDs for processes
int id_sem_var, id_sem_entry_pier, id_sem_start_row, status, id_sem_board_h, id_sem_board_s, id_sem_row_finished, id_sem_file;//IDs for shared memory segment for semaphores
int id_I_hacker, id_I_serf, id_NH, id_NS, id_in_boat, id_A;//IDs for shared memory segment for counter variables
int i,x,y;//Temp variables
srand(time(NULL));//Seeding random function
P = atoi(argv[1]);//retrieving values from command line
H = atoi(argv[2]);
S = atoi(argv[3]);
R = atoi(argv[4]);
if((argc!=5)||(P==0 || P%2!=0)|| (H<0 || H>5000)|| (S<0 || S>5000)|| (R<0 || R>5000))//Checking validity of user input
{
fprintf(stderr,"Invalid value of argument\n");
return 1;
}
H*=1000;//converting miliseconds to micro seconds, usleep() takes micro seconds as argument
S*=1000;
R*=1000;
fp = fopen("rivercrossing.out","w");//Opening file
if(fp==NULL)
{
fprintf(stderr,"Unable to access rivercrossing.out\n");
return 2;
}
//--------------------Creating shared memory segment of semaphores
id_sem_var = shmget(IPC_PRIVATE, sizeof(sem_t), (S_IRUSR|S_IWUSR));
id_sem_entry_pier = shmget(IPC_PRIVATE, sizeof(sem_t), (S_IRUSR|S_IWUSR));
id_sem_start_row = shmget(IPC_PRIVATE, sizeof(sem_t), (S_IRUSR|S_IWUSR));
id_sem_board_h = shmget(IPC_PRIVATE, sizeof(sem_t), (S_IRUSR|S_IWUSR));
id_sem_board_s = shmget(IPC_PRIVATE, sizeof(sem_t), (S_IRUSR|S_IWUSR));
id_sem_file = shmget(IPC_PRIVATE, sizeof(sem_t), (S_IRUSR|S_IWUSR));
id_sem_row_finished = shmget(IPC_PRIVATE, sizeof(sem_t), (S_IRUSR|S_IWUSR));
sem_var = (sem_t *)shmat(id_sem_var, NULL, 0);
sem_entry_pier = (sem_t *)shmat(id_sem_entry_pier, NULL, 0);
sem_start_row = (sem_t *)shmat(id_sem_start_row, NULL, 0);
sem_board_h = (sem_t *)shmat(id_sem_board_h, NULL, 0);
sem_board_s = (sem_t *)shmat(id_sem_board_s, NULL, 0);
sem_row_finished = (sem_t *)shmat(id_sem_row_finished, NULL, 0);
sem_file = (sem_t *)shmat(id_sem_file, NULL, 0);
//--------------------Creating shared memory segment of counters
id_A = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);
id_I_hacker = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);
id_I_serf = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);
id_NH = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);
id_NS = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);
id_in_boat = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);
A = (int *) shmat(id_A, 0, 0);
NH = (int *) shmat(id_NH, 0, 0);
NS = (int *) shmat(id_NS, 0, 0);
in_boat = (int *) shmat(id_in_boat, 0, 0);
//--------------------Initializing semaphores
sem_init(sem_var,1,1);
sem_init(sem_entry_pier,1,1);
sem_init(sem_start_row,1,0);
sem_init(sem_row_finished,1,0);
sem_init(sem_board_s,1,0);
sem_init(sem_board_h,1,0);
sem_init(sem_file,1,1);
sem_wait(sem_var);
//--------------------Initializing counters
*A=0;
*NH=0;
*NS=0;
*in_boat=0;
return 0;
}
这实现了称为rivercrossing的同步问题。
我编译gcc rivercrossing.c -o rc
并运行如下:./rc 2 2 2 200
其中第一个2
是每个类别中生成的人数(serf,黑客),第二个和第三个2
表示生成新进程黑客和serf的最大长度,最后200
是最大长度巡航。
输出应该在文件中
答案 0 :(得分:0)
您的一个或多个指针(A
,NH
,NS
或in_boat
)可能无效。这可能意味着shmat()
返回了其错误指示值(根据手册页为(void *)-1
)。您的代码应检查此错误指示值,并查看errno
以找出shmat()
失败的原因。