LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2021, 12:57 AM   #31
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,078

Rep: Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364

Quote:
Originally Posted by sundialsvcs View Post
Yeah, fork() – in both Unix® and Linux® – is a lot more expansive than the users of many other systems expect it to be, if they're accustomed to a mere "CreateProcess()" system function!
Just curious, how (or where?) can it be much better/faster?
 
Old 11-23-2021, 09:36 AM   #32
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,702
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
When you "fork" a process, you replicate everything about its context. A simple "create process" call in most OSes does not undertake to do nearly so much.
 
Old 11-23-2021, 02:07 PM   #33
Racho
Member
 
Registered: Oct 2021
Posts: 59

Rep: Reputation: Disabled
I read a bit about semaphores and I think that the problem is that you need to define the semaphore in shared memory so that parent and childs can use it.

I have modified your program following the example of the answer 8 of this post:
https://stackoverflow.com/questions/...same-semaphore

Code:
#include <sys/sem.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/mman.h>
int main()
{
    int pipe_fd[2];
    int status = 20;
    char buffer[4096]; //会初始化为0吗?Y
    pid_t pid1, pid2, pid3;
    sem_t *pipe_mutex = (sem_t *)mmap(NULL, sizeof(*pipe_mutex), 
		    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
    sem_init(pipe_mutex, 1, 1);

    printf("init\n");

    if ((pid1 = fork()) == 0)
    {

        printf("child1 init.\n");
        fflush(stdout);
        while (status >= 0)
        {
            sem_wait(pipe_mutex);
            fprintf(stdout, "child1 start writing\n");
            // fflush(stdout);
            // status = write(pipe_fd[1], buffer, 128);
            sleep(.2);
            fprintf(stdout, "child1 write %d\n", status);
            fflush(stdout);
            status--;
            sem_post(pipe_mutex);
            sleep(.1);
        }
        fflush(stdout);
        exit(0);
    }
    else if ((pid2 = fork()) == 0)
    {

        printf("child2 init.\n");
        fflush(stdout);
        while (status >= 0)
        {
            sem_wait(pipe_mutex);
            fprintf(stdout, "child2 start writing\n");
            // fflush(stdout);
            // status = write(pipe_fd[1], buffer, 2047);
            sleep(.2);
            fprintf(stdout, "child2 write %d\n", status);
            fflush(stdout);
            status--;
            sem_post(pipe_mutex);
            sleep(.1);
        }

        fflush(stdout);
        exit(0);
    }
    else if ((pid3 = fork()) == 0)
    {

        printf("child3 init.\n");
        fflush(stdout);
        while (status >= 0)
        {
            sem_wait(pipe_mutex);
            fprintf(stdout, "child3 start writing\n");
            // fflush(stdout);
            // status = write(pipe_fd[1], buffer, 4200);
            sleep(.2);
            fprintf(stdout, "child3 write %d\n", status);
            fflush(stdout);
            status--;
            sem_post(pipe_mutex);
            sleep(.1);
        }
        fflush(stdout);
        exit(0);
    }
    else
    {
        // getchar(); //这个语句在这里的作用是什么?type anything and enter to start
        //sem_post(pipe_mutex);
        wait(0);
        wait(0);
        wait(0);
        // pipe_left_size(pipe_fd, pipe_mutex); //这两个函数的作用又是什么?
        printf("end\n");
        sem_unlink("pipe_mutex");
    }

    return 0;
}
And the semaphores work as you can see in the output:
Code:
init
child1 init.
child1 start writing
child2 init.
child3 init.
child1 write 20
child2 start writing
child2 write 20
child3 start writing
child3 write 20
child1 start writing
child1 write 19
child2 start writing
child2 write 19
child3 start writing
child3 write 19
child1 start writing
child1 write 18
child2 start writing
child2 write 18
child3 start writing
child3 write 18
child1 start writing
child1 write 17
child2 start writing
child2 write 17
child3 start writing
child3 write 17
child1 start writing
child1 write 16
child2 start writing
child2 write 16
child3 start writing
child3 write 16
child1 start writing
child1 write 15
child2 start writing
child2 write 15
child3 start writing
child3 write 15
child1 start writing
child1 write 14
child2 start writing
child2 write 14
child3 start writing
child3 write 14
child1 start writing
child1 write 13
child2 start writing
child2 write 13
child3 start writing
child3 write 13
child1 start writing
child1 write 12
child2 start writing
child2 write 12
child3 start writing
child3 write 12
child1 start writing
child1 write 11
child2 start writing
child2 write 11
child3 start writing
child3 write 11
child1 start writing
child1 write 10
child2 start writing
child2 write 10
child3 start writing
child3 write 10
child1 start writing
child1 write 9
child2 start writing
child2 write 9
child3 start writing
child3 write 9
child1 start writing
child1 write 8
child2 start writing
child2 write 8
child3 start writing
child3 write 8
child1 start writing
child1 write 7
child2 start writing
child2 write 7
child3 start writing
child3 write 7
child1 start writing
child1 write 6
child2 start writing
child2 write 6
child3 start writing
child3 write 6
child1 start writing
child1 write 5
child2 start writing
child2 write 5
child3 start writing
child3 write 5
child1 start writing
child1 write 4
child2 start writing
child2 write 4
child3 start writing
child3 write 4
child1 start writing
child1 write 3
child2 start writing
child2 write 3
child3 start writing
child3 write 3
child1 start writing
child1 write 2
child2 start writing
child2 write 2
child3 start writing
child3 write 2
child1 start writing
child1 write 1
child2 start writing
child2 write 1
child3 start writing
child3 write 1
child1 start writing
child1 write 0
child2 start writing
child2 write 0
child3 start writing
child3 write 0
end

Last edited by Racho; 11-23-2021 at 02:46 PM.
 
Old 11-23-2021, 02:48 PM   #34
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,784

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Quote:
Originally Posted by sundialsvcs View Post
When you "fork" a process, you replicate everything about its context. A simple "create process" call in most OSes does not undertake to do nearly so much.
In modern processors, fork() isn't nearly as expensive as one might think. That replication is done by having the parent and child processes share the same memory pages and marking them "copy on write" in the MMU. The most common use for fork() is for the child process to do a little cleaning up and then call exec(), which discards almost all of the pages, so very little actual copying is performed.
 
1 members found this post helpful.
Old 11-23-2021, 06:01 PM   #35
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,702
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
@rknichols: Acknowledging that your assessment is (of course) technically correct.

While the Unix/Linux fork() procedure is not necessarily expensive, it is quite extensive. In most OSes, there is simply no corollary at all to fork().
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can I set the cups output order to always print in reverse order Thane Ubuntu 3 07-09-2018 09:49 PM
Logi Sales Manager on Ncurses (invoice, invoicing, orders, order, sale order, sales order...)? Xeratul Linux - Software 0 03-25-2017 02:45 PM
Why am I not able to redirect using htaccess. It says too many redirect rules. swathi.akkineni Linux - Newbie 1 07-31-2015 03:20 PM
How to make output redirect to a file and at the same time display on consloe? chinabenjamin66 General 1 05-01-2013 10:33 PM
Why the output files (trace file e.g. out.tr) are not same for the same tcl script ? askerwhat Ubuntu 1 06-03-2011 10:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration