how to create a process


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users how to create a process
# 1  
Old 04-16-2008
CPU & Memory how to create a process

Hell Sir,
This is chanikya
Is there any System call which behaves just like fork but i dont want to return back two times to the calling func.
In the following ex iam creating a child process in the called func but the ex prints two times IN MAIN.


ex :-
Code:
calling()
{
fork();

}

main()
{
calling();
printf("IN MAIN\n");
}


Last edited by Yogesh Sawant; 04-16-2008 at 04:37 AM.. Reason: added code tags
# 2  
Old 04-16-2008
check if exec is what you want
# 3  
Old 04-16-2008
Java exec

if we use exec the cursor wont come back
# 4  
Old 04-16-2008
You need to exec in the child process only. The manual page for fork() on your system probably has an example of exactly how to do this. Or google for fork exec.
# 5  
Old 04-16-2008
I'm attaching an example that allows you to invoke the various methods.
HTH.

<Edit: i tired to upload...oops...>
[code]
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define MAXARGS 20
#define MAXARGLEN 128

int dbg = 0;
int optopt, optind, opterr = 0;
char *optarg;

typedef struct action_ {
    int option; /*reserved*/
    char *progname;
    char *arg[MAXARGS];
} EXEC_ARG;


int do_thread_exec(void *);
void do_vfork_exec(void *);
void do_fork_exec(void *);
void *t_exec(void *);

/*options -a arguments string, -p program name for execution, -t, -v, -f*/
int main(int argc, char **argv)
{
    int opt, n = 0, xid;
    int thread, frk, vfrk;
    char *tmp;
    EXEC_ARG argument;

    if (argc < 3) {
	printf
	    ("Please remember to pass a program name via the -p option and any arguments to the program via -a.\nYou must specify an option from -f,-t,-v to execute anything.\n");
	exit(1);
    }

    thread = frk = vfrk = 0;
    while ((opt = getopt(argc, argv, "a:p:ftvd")) >= 0) {
	switch (opt) {
	case 'p':
	    argument.progname = malloc(strlen(optarg) + 1);
	    strcpy(argument.progname, optarg);
	    break;
	case 'a':

	    if ((tmp = strtok(optarg, " ")) == NULL) {
		argument.arg[n] = NULL;
		break;
	    }
	    argument.arg[n] = malloc(MAXARGLEN);
	    strncpy(argument.arg[n], tmp, MAXARGLEN);
	    n++;
	    while (n < (MAXARGS - 1)) {
		if ((tmp = strtok(NULL, " ")) == NULL) {
		    argument.arg[n] = NULL;
		    break;
		}
		argument.arg[n] = malloc(MAXARGLEN);
		if (dbg > 0) {
		    printf("Copying to %p with string %s at %d.\n",
			   &argument.arg[n], tmp, n);
		}
		strncpy(argument.arg[n], tmp, MAXARGLEN);
		n++;
	    }
	    break;
	case 'v':
	    vfrk = 1;
	    break;
	case 't':
	    thread = 1;
	    break;
	case 'f':
	    frk = 1;
	    break;
	case 'd':
	    dbg = 1;
	    break;
	}
    }

    if (frk > 0) {
	printf("Fork().\n");
	sleep(2);
	do_fork_exec(&argument);
	wait(NULL);
    }
    if (vfrk > 0) {
	printf("Vfork().\n");
	sleep(2);
	do_vfork_exec(&argument);
	wait(NULL);
    }
    if (thread > 0) {
	printf("Threaded...\n");
	sleep(2);
	xid = do_thread_exec(&argument);
	pthread_join(xid, NULL);
    }
    return 0;
}


void *t_exec(void *arg)
{
    EXEC_ARG *ptr = arg;
    execv(ptr->progname, ptr->arg);
    pthread_exit(NULL);
}

int do_thread_exec(void *arg)
{
    pthread_t tid;
    EXEC_ARG *ptr = arg;

    pthread_create(&tid, NULL, t_exec, ptr);
    return tid;
}

void do_vfork_exec(void *arg)
{
    EXEC_ARG *ptr = arg;
    if (vfork() == 0) {
	execv(ptr->progname, ptr->arg);
    }
    return;
}

void do_fork_exec(void *arg)
{
    EXEC_ARG *ptr = arg;
    if (fork() == 0) {
	execv(ptr->progname, ptr->arg);
    }
    return;
}

HTH.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Programming

Create a group of process

Hi all ! :) What I want? 1. All child process must be in the same group of process. Parent is a leader of the group. How to do this? I would be greatfull of some example of code, I read about setsid but I can't even start... My code so far: #include <stdio.h> #include <stdlib.h>... (2 Replies)
Discussion started by: mattdj
2 Replies

3. Shell Programming and Scripting

Process text file to create CSV

I am working on a text file where I have to get data from a text file and convert it into either CSV format or Column format as shown below. OUTPUT Expected GRP Name Pair Size DName DNumber PName PNumber adm_grp Pair1 150.00KG Pair_0ABC_1 0396 Pair_0267_s 1292 ... (6 Replies)
Discussion started by: shunya
6 Replies

4. Shell Programming and Scripting

Direction to create website to process grep and SED requests

hi I am seeking to create a cgi-bin type creation that will allow users browsing the site to be able to run searches that would be a grep command or SED in the backround. I am not sure how to go about this, if you would give me a pointer or direction about what technology i could inform myself... (0 Replies)
Discussion started by: cdc01
0 Replies

5. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

6. Solaris

Cannot create slave process

Hi I have solaris 10 system. I try get backup of one mount point to LTO2 tape drive which is attached to another machine but backup aborted.(restoring files to another mount point at same time) . can anybody help me DUMP: Mapping (Pass I) DUMP: Mapping (Pass II) DUMP: Writing 63... (1 Reply)
Discussion started by: lskod
1 Replies

7. Shell Programming and Scripting

Create a html file if a process is running??

Hi All, I need to check for a process, if the process is running then I have to create an HTML file, say A.HTML. If the process is not running then I have to rename the existing html, say A.HTML to B.HTML so that the process which looks for the file A.HTML does not find it? How do I do... (1 Reply)
Discussion started by: Hangman2
1 Replies

8. UNIX for Dummies Questions & Answers

How do you create a zombie process?

I'm very new to UNIX, so I need some help please. How do I create a zombie process with just basic UNIX commands (no script, C, PERL, etc)? Please give an example. Thanks. (6 Replies)
Discussion started by: teiji
6 Replies

9. UNIX for Advanced & Expert Users

How to create a dummy process of a process already running?

Hi Everybody, I want to create a shell script named as say "jip" and it is runned. And i want that when i do ps + grep for the process than this jip should be shown as process. Infact there might be process with name jip which is already running. (3 Replies)
Discussion started by: shambhu
3 Replies

10. Programming

How to create constantly running process

Ther are two process in my program and i want both to constantly running. So i have written the following code. But one of this process which is calling function wsJobCheck() is getting terminated with giving message : Program exited normally. Can any one suggest why this is happing. Code : ... (1 Reply)
Discussion started by: bhakti
1 Replies
Login or Register to Ask a Question