HPE GreenLake Administration
- Community Home
 - >
 - Servers and Operating Systems
 - >
 - Operating Systems
 - >
 - Operating System - HP-UX
 - >
 - Re: How to invoke another c program inside a c pro...
 
Operating System - HP-UX
        1840126
        Members
    
    
        4822
        Online
    
    
        110161
        Solutions
    
Forums
        Categories
Company
Local Language
                
                  
                  back
                
        
                
        
                
        
                
        
        
        
                
        
                
        
        
        
                
        
              
              Forums
Discussions
Forums
- Data Protection and Retention
 - Entry Storage Systems
 - Legacy
 - Midrange and Enterprise Storage
 - Storage Networking
 - HPE Nimble Storage
 
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
                
                  
                  back
                
            
                
            
                
            
                
            
                
            
                
            
                
            
                
            
                
            
                
            
                
            
                
            
                
            
                
            
                
            
                
            
                
            
            
                
            
                
            
                
            
                
            
            
                
            
                
            
            
                
            
                
              
            Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
 - Appliance Servers
 - Alpha Servers
 - BackOffice Products
 - Internet Products
 - HPE 9000 and HPE e3000 Servers
 - Networking
 - Netservers
 - Secure OS Software for Linux
 - Server Management (Insight Manager 7)
 - Windows Server 2003
 - Operating System - Tru64 Unix
 - ProLiant Deployment and Provisioning
 - Linux-Based Community / Regional
 - Microsoft System Center Integration
 
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
        Information
        Community
Resources
Community Language
        Language
        Forums
Blogs
	
		
			
            
                
            Go to solution
        
            
		
		
			
            	
	
		
        
		
	
	
		Topic Options
			
				
					
	
			
		
	- Subscribe to RSS Feed
 - Mark Topic as New
 - Mark Topic as Read
 - Float this Topic for Current User
 - Bookmark
 - Subscribe
 - Printer Friendly Page
 
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
02-02-2001 09:18 AM
02-02-2001 09:18 AM
			
				
					
					
						As a control c program, it checks some condition.  When the condition exists, it should kick of another processing c program.  What command/code I should use inside the control c program to start the processing c program?
Thanks.
					
				
			
			
				
			
			
				
	
			
				
		
			
			
			
			
			
			
		
		
		
	
	
	
Thanks.
Solved! Go to Solution.
		2 REPLIES 2
	
	            
            
		
		
			
            
                - Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
02-02-2001 10:43 AM
02-02-2001 10:43 AM
Solution
			
				
					
					
						There are actually 3 different methods for starting up a program from within another program, depending on what you want to do with the program in question.
First, you can use the system() call. This will execute another program, wait for it to terminate, and then return the exit code of that application to your program. For example:
system( "/usr/bin/ls" );
This will execute /usr/bin/ls and your program will wait until the "ls" program terminates.
The second option is to use the exec() family of functions, (usually in conjunction with fork()). fork() will create a new process (which can run independently from its parent), and exec() causes the currently running process to be replaced with the program specified. For example:
    
if ( fork() == 0 ) {
/* First child */
setpgrp();
fclose( stdin );
fclose( stdout );
fclose( stderr );
if ( fork() ) exit(0);
/* second child */
exec( "/usr/local/bin/myprogram" );
}
The above code is similar to what is found in many daemon processes -- this will start a program completely independent of its parent.
The third option involves using popen() to open a process and redirect its standard input and/or output to a file descriptor that we can read and/or write to. This is useful if you want to actually examine the output of that command. For example:
fp = popen( "/usr/bin/ls", "r" );
while( !feof( fp ) ) {
fgets( buf, bufsiz, fp );
/* Do useful stuff here */
}
I hope this helps.
		
		
	
	
	
First, you can use the system() call. This will execute another program, wait for it to terminate, and then return the exit code of that application to your program. For example:
system( "/usr/bin/ls" );
This will execute /usr/bin/ls and your program will wait until the "ls" program terminates.
The second option is to use the exec() family of functions, (usually in conjunction with fork()). fork() will create a new process (which can run independently from its parent), and exec() causes the currently running process to be replaced with the program specified. For example:
if ( fork() == 0 ) {
/* First child */
setpgrp();
fclose( stdin );
fclose( stdout );
fclose( stderr );
if ( fork() ) exit(0);
/* second child */
exec( "/usr/local/bin/myprogram" );
}
The above code is similar to what is found in many daemon processes -- this will start a program completely independent of its parent.
The third option involves using popen() to open a process and redirect its standard input and/or output to a file descriptor that we can read and/or write to. This is useful if you want to actually examine the output of that command. For example:
fp = popen( "/usr/bin/ls", "r" );
while( !feof( fp ) ) {
fgets( buf, bufsiz, fp );
/* Do useful stuff here */
}
I hope this helps.
	I think, therefore I am... I think!
			
			
				
			
			
			
			
			
			
		- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
02-02-2001 01:21 PM
02-02-2001 01:21 PM
			
				
					
						
							Re: How to invoke another c program inside a c program
						
					
					
				
			
		
	
			
	
	
	
	
	
			
				
					
					
						Thanks for quick response.  The following function in a c program worked:
void startprocs()
{
if ( fork() == 0 )
execlp("procsudf.exe", (char *)0);
}
		
		
	
	
	
void startprocs()
{
if ( fork() == 0 )
execlp("procsudf.exe", (char *)0);
}
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
		
	
	
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP