Alright.. I have to rewrite a program. The thing is, I have to rewrite it using parent and children... With processes (pipes) or threads...

How do you create the parents and children with either of these?

I am basically asking for a tutorial on how you would create these with either processes or pipes... in C.

Could I have an example please?

We got absolutely no examples in class and I would really appreciate some guidance!

Thanks.

Dani AI

Generated

A compact, practical primer tying the earlier hints together (from , , and ) with short examples, common pitfalls, and a couple of C/C++ notes (also relevant to and ).

Process-based pattern (Unix): create a pipe, fork(), close unused ends, use dup2() in the child to redirect stdin/stdout and exec() a new program; parent writes/reads and waits to avoid zombies. Common pitfalls: forgeting to close the unused pipe end (causes hangs), not checking return values, and assuming unlimited pipe buffer (writes can block).

int fd[2];
pipe(fd);
pid_t pid = fork();
if (pid == 0) {
    close(fd[1]);
    dup2(fd[0], STDIN_FILENO);
    close(fd[0]);
    execlp("childProg","childProg",(char*)NULL);
    _exit(127);
} else {
    close(fd[0]);
    write(fd[1], buf, len);
    close(fd[1]);
    waitpid(pid, NULL, 0);
}

Thread-based pattern (POSIX): create worker threads with pthread_create, protect shared data with mutexes/conds, and join threads when finished. Watch for library thread-safety and race conditions; prefer condition variables for coordination and use -pthread at compile/link time.

void *worker(void *arg) { /* safe shared-data access with mutex */ return NULL; }
pthread_t t;
pthread_create(&t, NULL, worker, arg);
pthread_join(t, NULL);

C vs C++ and portability notes: C APIs are usable from C++ — wrap C headers in extern "C" { ... } to avoid name-mangling when required. On Windows use CreateProcess/CreateThread (API differences noted by ). Avoid calling non‑async‑safe functions in a child created by fork in a multi-threaded program; prefer posix_spawn or fork+exec.

Quick checklist: decide isolation vs shared-memory, sketch IPC and lifetime, check all return values, close unused descriptors, protect shared state with mutexes, run under tools like valgrind/Helgrind or ThreadSanitizer, and test edge cases (partial reads/writes, SIGCHLD handling).

Recommended Answers

All 9 Replies

I can tell you on unix. To create a parent, you run a process.
To create a child for this process, use fork.
further is family of exec() function's.

See "Chapter 2: Processes and Threads" for lecture notes with sample code, and see "Writing a Shell" (which mentions pipes and reading and writing), with sample code.

(Using Unix of course.)

thanks for the help guys... ill get to reading..

and it was for unix (sorry i didnt specify)

i have to create a perent/children process in c++... is there any way to do it as in c?

Yeah, just use the same methods, you can link to C libraries from a C++ executable as well. I.e. use C apis in C++ code also. I think on unix the apis are in pthread lib if you want POSIX

so, all i have to do to use a c library is copy the *.c file into the directory where i have my .cpp and call it in the program with an #include "*.c" ?

so, all i have to do to use a c library is copy the *.c file into the directory where i have my .cpp and call it in the program with an #include "*.c" ?

You never #include .c files. To use a library, the general steps are:

  1. Copy all the header files (.h) into a general directory that the compiler reads.
  2. Copy the library files (.lib) into another directory that the linker reads.
  3. In your project, you have to make sure that the compiler and linker paths are setup to the directories where you put the .libs and headers files.
  4. #include all the header files in your source code.
  5. Add the names of the .libs in your linker options.

And dynamic libraries have .dlls that your program has to link to when it runs, but your OS takes care of that (usually).

You should also note that steps 1-2 are usually completed by the installer when you download the library.

hmm... but, since my conio library didn't work correctly (Using Dev's c++), i managed to obtain a new library, with which i have to #include the "conio.c" file, instead of the usual <conio.h> file... and that's how i use my conio library ever since...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.