我想用寫一個 shared memory 寫出下列數字
35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1 (n是偶數 /2 n是奇數 *3+1)
我的程式 跑出來Segmentation fault 再加 14個亂數
可以幫我看看嗎 ??
#include<stdio.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<string.h>
#include<fcntl.h>
int main ()
{
const char *name ="OS";
const int SIZE =4096;
int shm_fd;
int pid;
int i;
int number =35;
char * shared_memory[14];
shm_fd = shm_open(name, O_CREAT|O_RDWR, 0666);
ftruncate(shm_fd, SIZE)
shared_memory[14] = (char *) mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd,0);
if(pid==0) {
i=0;
while(number !=1) {
if(number%2 ==0)
number =number/2;
else
number =3*number+1;
} wait(NULL);
} else if (pid ==0) {
sleep(1);
for(i=0;i<14;i++)
printf("%d\n", shared_memory[i]);
exit(0);
}
shm_unlink(name);
return 0;
}
This entry passed through the Full-Text RSS service — if this is your content and you're reading it on someone else's site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.