使用共享内存的方式实现进程间通信,读取数据时报“总线错误”。感觉有点意思

**
#include // exit()
#include //fork()
#include // pid_t
#include // wait()
#include // printf()
#include
#include
#include
#include // shm_open(), ftruncate(), mmap(), shm_unlink()
#include
int main(int argc, char* argv[]){

const int SIZE = 4096;
const char *name = "OS";
static int count = 0;
if(argc !=2 ){
    perror("usage error");
    exit(-1);
}
int num = atoi(argv[1]);

int shm_fd;
void *ptr;
shm_fd = shm_open(name, O_CREAT, 0666);
ftruncate(shm_fd, SIZE);

if( num > 0)
{
    pid_t pid;
    pid = fork();
    if(pid == 0)
    {
        int shm_fd;
        void *ptr;
        shm_fd = shm_open(name, O_RDWR, 0666);
        ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);

        sprintf(ptr, "%d", num);
        ptr += sizeof(int);
        count ++;

        while(num != 1)
        {
            num = (num%2 == 0 ? num/2 : 3*num+1);
            sprintf(ptr, "%d", num);
            ptr += sizeof(int);
            count++;
        }
    }
    else if(pid >0)
    {
        wait(NULL);

        int shm_fd;
        void *ptr;
        shm_fd = shm_open(name, O_RDONLY, 0666);
        ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
        int *p = (int*) ptr;
        printf("%d ", *p++);
        while(count > 0){
            printf("%d ",*p++);
            count --;
        }

        shm_unlink(name);
        printf("\n");
    }
    else
    {
        perror("fork error");
        exit(-1);
    }   
}
else
{
    perror("please input a positive interge");
    exit(-1);
}   

return 0;

}
**

2个回答

很感谢你的链接,但是我目前也不知道怎么解决这个问题。
1。Linux与Window的进程关系是不同的
2。这里是总线错误,应该是字节不对齐所致(用了指针)
3。下面的3.22题是原题,(3.21与3.22有关)。题目意思比较明确,但是自己做错了。
如果能给出解答,不胜感激
3.21
The Collatz conjecture concerns what happens when we take any
positive integer n and apply the following algorithm:
n =
n/2,
if n is even
3 × n + 1, if n is odd
The conjecture states that when this algorithm is continually applied,
all positive integers will eventually reach 1. For example, if n = 35, the
sequence is
35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1
Write a C program using the fork() system call that generates this
sequence in the child process. The starting number will be provided
from the command line. For example, if 8 is passed as a parameter on
the command line, the child process will output 8, 4, 2, 1. Because the
parent and child processes have their own copies of the data, it will be
necessary for the child to output the sequence. Have the parent invoke
the wait() call to wait for the child process to complete before exiting
the program. Perform necessary error checking to ensure that a positive
integer is passed on the command line.
3.22
In Exercise 3.21, the child process must output the sequence of numbers
generated from the algorithm specified by the Collatz conjecture because
the parent and child have their own copies of the data. Another
approach to designing this program is to establish a shared-memory
object between the parent and child processes. This technique allows the
child to write the contents of the sequence to the shared-memory object.
The parent can then output the sequence when the child completes.
Because the memory is shared, any changes the child makes will be
reflected in the parent process as well.
This program will be structured using POSIX shared memory as
described in Section 3.5.1. The parent process will progress through the
following steps:
a. Establish the shared-memory object ( shm open() , ftruncate() ,
and mmap() ).
b. Create the child process and wait for it to terminate.
c. Output the contents of shared memory.
d. Remove the shared-memory object.
One area of concern with cooperating processes involves synchro-
nization issues. In this exercise, the parent and child processes must be
coordinated so that the parent does not output the sequence until the
child finishes execution. These two processes will be synchronized using
the wait() system call: the parent process will invoke wait() , which
will suspend it until the child process exits.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!