Sponsored Content
Top Forums Programming alternatives of exec() system function Post 302155102 by porter on Thursday 3rd of January 2008 01:27:57 AM
Old 01-03-2008
Quote:
Originally Posted by Raj Kumar Arora
And I dont want to create a new process in a thread.
The function system() takes a string which will be parsed by the shell, this can have as many arguments as the system will permit.

Under UNIX, the only way you can run a program is by "exec" which replaces the current program image with the new program.

If you want your program to continue running you have to call fork() or vfork() to create a new process, tough, but that is UNIX.

Yes, threads and fork can be a nightmare, what exactly is the problem? If it's resource contention/locking I suggest you have a look at "pthread_atfork()".

If you need to spawn a new process from a multithreaded program you have no options other than

(a) using fork/vfork
(b) calling a a function that wraps them up
(c) use IPC to talk to another program to create the new process
 

10 More Discussions You Might Find Interesting

1. Programming

exec() system call

hi there, i was reading about the exec() function. and if i m not wrong, exec() kills your present process and starts a new process in its place. the process id remains the same. then it says if exec is successful the text data and stack are overlayed by new file! - i dont get this part "only... (2 Replies)
Discussion started by: a25khan
2 Replies

2. UNIX for Advanced & Expert Users

Alternatives to set the system date ??

Hi all, I need to syncronize a Solaris client with a QNX Server, modifying the client date, I need any alternative to set the sistem date (client Solaris) but i can't use commands date -a XXX (XXX are the time in seconds) and can't use rdate and ntp. How can I do It? :confused: (2 Replies)
Discussion started by: ulisses0205
2 Replies

3. Shell Programming and Scripting

How can I execute own ksh function in find -exec

Hi, I wrote a smiple ksh function send_notification() { ... } and want to execute it on each file, matched by the find command. I tried: find / -name "*.err" -mtime -8 -exec send_notification {} \; but it doesn't work. What should I do? I work in ksh on Hp-Ux. Regards, Pit (11 Replies)
Discussion started by: piooooter
11 Replies

4. UNIX for Advanced & Expert Users

exec to call specific function in C prog

I would like to call a particular function in a C program using execl(). Is this possible using execl or anyother function ? Thanks (2 Replies)
Discussion started by: vpraveen84
2 Replies

5. Programming

Runtime.getSystem.exec() function waits for child

Runtime.getSystem.exec() waits for child process to complete .. I do not want to wait for the child process to complete ..what is the option which has to be used ? (2 Replies)
Discussion started by: shafi2all
2 Replies

6. Shell Programming and Scripting

How to execute piped command using exec or system

Hi All, I want to execute a piped command like 'ls /opt | grep xml' using array as parameters list. How can I do that? (2 Replies)
Discussion started by: bharadiaam
2 Replies

7. Shell Programming and Scripting

Perl variables in exec or system

I am new in Perl. I am working in simple script and the varibles are working well outside the exec or system command. but they don't work as parameters to exec or system command. The script is attached. please help. (8 Replies)
Discussion started by: ahmed_zaher
8 Replies

8. UNIX for Dummies Questions & Answers

Using system function in C

Hi Guys , I want to use system function in C to do the following work. cp <file1> <file2> and then ><file1> e,g cp \var\log\cpm_cpmd_1.log.1 \var\log\cpm_cpmd_1.log.2 and then >\var\log\cpm_cpmd_1.log.1 1. g_config_info.cpmm_config.cpm_log_path=\var\log\ 2. ... (3 Replies)
Discussion started by: meet123321
3 Replies

9. Programming

[C] exec system call

Hi again ;) Now I want to make a program that will execute the programs with exec, asking the user if he wants the program to run in background or foreground. scanf("%c",&caracter); if (caracter=='y'){ printf("Has decidido ejecutarlo en background\n"); if((pid=fork())==0) {// fork para... (3 Replies)
Discussion started by: lamachejo
3 Replies

10. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies
vfork(2)							   System Calls 							  vfork(2)

NAME
vfork, vforkx - spawn new process in a virtual memory efficient way SYNOPSIS
#include <unistd.h> pid_t vfork(void); #include <sys/fork.h> pid_t vforkx(int flags); DESCRIPTION
The vfork() and vforkx() functions create a new process without fully copying the address space of the old process. These functions are useful in instances where the purpose of a fork(2) operation is to create a new system context for an execve() operation (see exec(2)). Unlike with the fork() function, the child process borrows the parent's memory and thread of control until a call to execve() or an exit (either abnormally or by a call to _exit() (see exit(2)). Any modification made during this time to any part of memory in the child process is reflected in the parent process on return from vfork() or vforkx(). The parent process is suspended while the child is using its resources. In a multithreaded application, vfork() and vforkx() borrow only the thread of control that called vfork() or vforkx() in the parent; that is, the child contains only one thread. The use of vfork() or vforkx() in multithreaded applications, however, is unsafe due to race condi- tions that can cause the child process to become deadlocked and consequently block both the child and parent process from execution indefi- nitely. The vfork() and vforkx() functions can normally be used the same way as fork() and forkx(), respectively. The calling procedure, however, should not return while running in the child's context, since the eventual return from vfork() or vforkx() in the parent would be to a stack frame that no longer exists. The _exit() function should be used in favor of exit(3C) if unable to perform an execve() operation, since exit() will invoke all functions registered by atexit(3C) and will flush and close standard I/O channels, thereby corrupting the par- ent process's standard I/O data structures. Care must be taken in the child process not to modify any global or local data that affects the behavior of the parent process on return from vfork() or vforkx(), unless such an effect is intentional. Unlike fork() and forkx(), fork handlers are not run when vfork() and vforkx() are called. The vfork() and vforkx() functions are deprecated. Their sole legitimate use as a prelude to an immediate call to a function from the exec family can be achieved safely by posix_spawn(3C) or posix_spawnp(3C). Fork Extensions The vforkx() function accepts a flags argument consisting of a bitwise inclusive-OR of zero or more of the following flags, which are defined in the header <sys/fork.h>: FORK_NOSIGCHLD FORK_WAITPID See fork(2) for descriptions of these flags. If the flags argument is 0, vforkx() is identical to vfork(). RETURN VALUES
Upon successful completion, vfork() and vforkx() return 0 to the child process and returns the process ID of the child process to the par- ent process. Otherwise, -1 is returned to the parent process, no child process is created, and errno is set to indicate the error. ERRORS
The vfork() and vforkx() functions will fail if: EAGAIN The system-imposed limit on the total number of processes under execution (either system-quality or by a single user) would be exceeded. This limit is determined when the system is generated. ENOMEM There is insufficient swap space for the new process. The vforkx() function will fail if: EINVAL The flags argument is invalid. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Obsolete | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
exec(2), exit(2), fork(2), ioctl(2), atexit(3C), exit(3C), posix_spawn(3C), posix_spawnp(3C), signal.h(3HEAD), wait(3C), attributes(5), standards(5) NOTES
To avoid a possible deadlock situation, processes that are children in the middle of a vfork() or vforkx() are never sent SIGTTOU or SIGT- TIN signals; rather, output or ioctls are allowed and input attempts result in an EOF indication. To forestall parent memory corruption due to race conditions with signal handling, vfork() and vforkx() treat signal handlers in the child process in the same manner as the exec(2) functions: signals set to be caught by the parent process are set to the default action (SIG_DFL) in the child process (see signal.h(3HEAD)). Any attempt to set a signal handler in the child before execve() to anything other than SIG_DFL or SIG_IGN is disallowed and results in setting the handler to SIG_DFL. On some systems, the implementation of vfork() and vforkx() cause the parent to inherit register values from the child. This can create problems for certain optimizing compilers if <unistd.h> is not included in the source calling vfork() or if <sys/fork.h> is not included in the source calling vforkx(). SunOS 5.11 13 Dec 2006 vfork(2)
All times are GMT -4. The time now is 07:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy