English 中文(简体)
C. 气温同步过程
原标题:Synchronising processes in C, linux

The problem is more difficult, but I want to understand an easier example. Let s say I have 3 processes, and I want process 3 to start before process 1, how can I do it? Or, is it possible to do it?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <semaphore.h>

int main() {
    sem_t semaphore;

    // Initialize semaphore with initial value 0
    if (sem_init(&semaphore, 0, 0) == -1) {
        perror("Semaphore initialization failed");
        exit(EXIT_FAILURE);
    }

    // Create child process 1
    if (fork() == 0) {
        // Child process 1 (Process 1) starts here
        printf("Process 1 started.
");
        // Wait for signal from Process 3
        sem_wait(&semaphore); 
        printf("Process 1 completed its work.
");
        exit(0);
    }

    // Create child process 2
    if (fork() == 0) {
        // Child process 2 (Process 2) starts here
        printf("Process 2 started.
");
        printf("Process 2 completed its work.
");
        exit(0);
    }

    // Create child process 3
    if (fork() == 0) {
        // Child process 3 (Process 3) starts here
        printf("Process 3 started.
");
        // Signal Process 1 to start
        sem_post(&semaphore);
        exit(0);
    }

    wait(NULL);
    wait(NULL);
    wait(NULL);

    // Destroy semaphore
    sem_destroy(&semaphore);

    return 0;
}

这是我获得的产出:

Process 1 started.
Process 3 started.
Process 2 started.
Process 2 completed its work.

它像陷入无限的循环,进程1和进程3并不终止。

最佳回答

You need to use a "shared" semaphore to share/signal between processes. An unshared semaphore (like you re using) can only synchronize threads in a single process.

To create a shared semaphore, you need to allocate it in shared memory, which is a bit tricky. The easiest way is to use

#include <sys/mman.h>

    :

    sem_t *semaphore = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE,
                            MAP_SHARED|MAP_ANONYMOUS, -1, 0);
    if (semaphore == MAP_FAILED || sem_init(semaphore, 1, 0) == -1) {
        perror("Semaphore initialization failed");
        exit(EXIT_FAILURE);
    }

这是一种微小的低效,因为它将一整页(通常为4096英特)分配给ema,其中多数(地图将按要求将大小四舍五入到一页数)。

如果你需要多头ema,你可以把他们作为一阵列分配,只用一只冲锋枪。 如果你们需要更多的共同记忆(例如,各进程之间沟通的缓冲),你就可以把你需要的所有东西集中起来,形成一个单一的大方向,并配以<条码>的<<>mmap>。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签