Help: how to call fork() in shell script? New to linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help: how to call fork() in shell script? New to linux
# 1  
Old 09-11-2012
Help: how to call fork() in shell script? New to linux

Hi, I'm writing a shell script where I want to call fork(). However I wrote like this "var=fork()" in c style and got this error:
"syntax error near unexpected token `(' "

How could I call fork() in shell script? Thanks in advance.

Moderator's Comments:
Mod Comment Duplicate Post - Continue Here - Please Do Not Cross Post or Double Post!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Help: how to call fork() in shell script? New to linux

Hi, I'm writing a shell script where I want to call fork(). However I wrote like this "var=fork()" in c style and got this error: "syntax error near unexpected token `(' " How could I call fork() in shell script? Thanks in advance. (2 Replies)
Discussion started by: Xiaoya
2 Replies

2. Shell Programming and Scripting

how to suppress the status of fork in shell script

When command is executed by forking, the console displays the status of that command. I want to suppress it.. how to do it ? Example: var1=`date` & echo "hello world"; output: hello world + Done var1=`date` I want to suppress the second line "+ Done var1=`date`". I... (10 Replies)
Discussion started by: Arun_Linux
10 Replies

3. UNIX for Advanced & Expert Users

Doubt with fork() system call

Hi I wrote a simple fork program to illustrate the fork() system cal. here it is #include<stdio.h> #include<sys/stat.h> #include<sys/types.h> main() { int flag; flag=fork(); if(flag==0) { printf("Child \n"); printf("Process id= %d\n",getpid()); ... (3 Replies)
Discussion started by: badsha6642
3 Replies

4. Programming

Problem with execution of fork system call if i use \n

hi all, i tried the following source codes: fork1.c: main() { printf("demo of fork\n"); fork(); printf("hello"); } output: demo of fork hello hello fork2.c: main() (3 Replies)
Discussion started by: pnirmala
3 Replies

5. Shell Programming and Scripting

fork system call and \n

hi, i tried the following source codes: fork1.c: main() { printf("demo of fork\n"); fork(); printf("hello"); } output: demo of fork hello hello fork2.c: main() { printf("demo of fork"); (0 Replies)
Discussion started by: pnirmala
0 Replies

6. Homework & Coursework Questions

fork system call understanding

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: i have a problem in understanding the behaviour of fork . i understood fork as to create a new process and... (4 Replies)
Discussion started by: MrUser
4 Replies

7. UNIX for Dummies Questions & Answers

fork system call

Hi folks, I want to know how this below program works? #include <stdio.h> int main() { printf("A\n"); fork(); printf("B\n"); fork(); fork(); printf("D\n"); fork(); printf("C\n"); } This is just for example. How this type of programs where fork is used many places, how the... (1 Reply)
Discussion started by: u_peerless
1 Replies

8. UNIX for Dummies Questions & Answers

fork() system call

Can anyone explain me what really happens when a system call fork() is called ? I like to know what happens internally. Thanks in Advance. - Arun (1 Reply)
Discussion started by: arunviswanath
1 Replies

9. Programming

Fork() system call time?

One more question. How can i calculate the time that system needs to make fork() system call? I need to make it with times function but i really don't know how. :( (2 Replies)
Discussion started by: davidoff
2 Replies

10. UNIX for Advanced & Expert Users

URGENT Help required regarding the use of FORK system call

I desperately wanted one of the UNIX Gurus to help me resolve my problem asap(I have to deliver the code to the client by Monday 08-oct). I have a file with around 5 million records (50 lakhs). Now my original process was taking around 30 hours to read the complete file, process each and every... (4 Replies)
Discussion started by: kkumar1975
4 Replies
Login or Register to Ask a Question
pthread_atfork(3C)					   Standard C Library Functions 					pthread_atfork(3C)

NAME
pthread_atfork - register fork handlers SYNOPSIS
#include <sys/types.h> #include <unistd.h> int pthread_atfork(void (*prepare) (void), void (*parent) (void), void (*child) (void)); DESCRIPTION
The pthread_atfork() function declares fork handlers to be called prior to and following fork(2), within the thread that called fork(). The order of calls to pthread_atfork() is significant. Before fork() processing begins, the prepare fork handler is called. The prepare handler is not called if its address is NULL. The parent fork handler is called after fork() processing finishes in the parent process, and the child fork handler is called after fork() processing finishes in the child process. If the address of parent or child is NULL, then its handler is not called. The prepare fork handler is called in LIFO (last-in first-out) order, whereas the parent and child fork handlers are called in FIFO (first-in first-out) order. This calling order allows applications to preserve locking order. RETURN VALUES
Upon successful completion, pthread_atfork() returns 0. Otherwise, an error number is returned. ERRORS
The pthread_atfork() function will fail if: ENOMEM Insufficient table space exists to record the fork handler addresses. USAGE
Solaris threads do not offer pthread_atfork() functionality (there is no thr_atfork() interface). However, a Solaris threads application can call pthread_atfork() to ensure fork()-safety, since the two thread APIs are interoperable. Seefork(2) for information relating to fork() in a Solaris threads environment in Solaris 10 relative to previous releases. EXAMPLES
Example 1 Make a library safe with respect to fork(). All multithreaded applications that call fork() in a POSIX threads program and do more than simply call exec(2) in the child of the fork need to ensure that the child is protected from deadlock. Since the "fork-one" model results in duplicating only the thread that called fork(), it is possible that at the time of the call another thread in the parent owns a lock. This thread is not duplicated in the child, so no thread will unlock this lock in the child. Deadlock occurs if the single thread in the child needs this lock. The problem is more serious with locks in libraries. Since a library writer does not know if the application using the library calls fork(), the library must protect itself from such a deadlock scenario. If the application that links with this library calls fork() and does not call exec() in the child, and if it needs a library lock that may be held by some other thread in the parent that is inside the library at the time of the fork, the application deadlocks inside the library. The following describes how to make a library safe with respect to fork() by using pthread_atfork(). 1. Identify all locks used by the library (for example {L1,...Ln}). Identify also the locking order for these locks (for example {L1...Ln}, as well.) 2. Add a call to pthread_atfork(f1, f2, f3) in the library's .init section. f1, f2, f3 are defined as follows: f1() { /* ordered in lock order */ pthread_mutex_lock(L1); pthread_mutex_lock(...); pthread_mutex_lock(Ln); } f2() { pthread_mutex_unlock(L1); pthread_mutex_unlock(...); pthread_mutex_unlock(Ln); } f3() { pthread_mutex_unlock(L1); pthread_mutex_unlock(...); pthread_mutex_unlock(Ln); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
exec(2), fork(2), atexit(3C), attributes(5), standards(5) SunOS 5.11 12 Dec 2003 pthread_atfork(3C)