Sponsored Content
Full Discussion: background process
Top Forums Programming background process Post 36498 by badshah on Friday 30th of May 2003 05:19:34 AM
Old 05-30-2003
background process

I have made a shell that accept a command and parameters. It is working properly. I have tryed to implement background process in main(). But i dont know to implement them. Can anyone give me a lille example??

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* The following define's belongs to read_command(..) */
#define MAXPARMS	20
#define MAXPARMLEN	255
#define MAXINPUT	255
#define shell		"cesh"

/* The Command line parser */

/*------------------------------------------------------------------*/
/* read_command:                                                    */
/*   Print the prompt and get one characters from the users which   */
/*   makes up an entire command line entry                          */
/*------------------------------------------------------------------*/

void read_command(char * command, char * parameters[])
{
  char input[MAXINPUT];
  char * parm;
  char c;
  int i;

  /* initialize the whole parameters array to NULL pointers */
  for (i=0;i<MAXPARMS;i++)
    {
      parameters[i]=NULL;
    }

  i=0;
  input[0]='\0';

  /* loop until some characters are entered on the command line */
  do
    {
      printf("%s> ", shell);
      c=getchar();
      while ((c != '\n') && (i < MAXINPUT-1))
        {
          input[i]=c;
          i++;
          c=getchar();
        }
      input[i]='\0';
    } while (strlen(input)==0);

  /* copy the first "word" on the command line into the command var */
  /* this also initializes the use of the strtok function */
  strcpy(command, strtok(input, " "));
  parameters[0]=malloc((MAXPARMLEN+1)*sizeof(char));
  strcpy(parameters[0], command);
  i=1;

  /* separate all of the "words" on the command line. Each becomes */
  /* a string pointed to by the parameters array using strtok. */
  do
    {
      if (parm=strtok(NULL, " "))
        {
          parameters[i]=malloc((MAXPARMLEN+1)*sizeof(char));
          strcpy(parameters[i],parm);
        }
      else
        {
          parameters[i]=NULL;
        }
      i++;
    } while((parameters[i-1]!=NULL) && (i < MAXPARMS));

  return;
}

int main(void)
{
char name[10];
char *argv[MAXPARMS];

read_command(name, argv);

/*background process*/
int pid, status;
int BGRND = 0;

do 
{
	if (strcmp(argv[0], "&")==0))
	{
		BGRND = 1;
	}
	if ( (pid = fork() < 0 )
			return 0;

	if(pid == 0 )
	{
		if ((execvp(argv[0], argv)) == -1)
			printf("%s: Command not found.\n", tokenstr);
			return 0;
	}
	if(BGRND == 0)
	{
		while (waitpid(-1, &status, 0) != pid);
	}
	BGRND = 0;
}while(strcmp(name,"exit));

return 0;
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies

2. Shell Programming and Scripting

process in background

Hi Guys, I am facing some weird problem with my shell script. The script shows up a menu and for every ontion internally calls a shell script which start/stop various servers. When I am using '&' while calling the internal shell script so that the server run in the background. For exiting... (1 Reply)
Discussion started by: agoyal
1 Replies

3. Shell Programming and Scripting

background process

Hi, In shell script when I use script1 >> filelog the echo statments of script1 gets printed in the filelog but when I try to run script in background i.e, script1 & >> filelog nothing gets printed in the filelog. Anybody knows whats going on here. thanks (3 Replies)
Discussion started by: k_oops9
3 Replies

4. UNIX for Dummies Questions & Answers

background process

How, can I hide background process's output? (5 Replies)
Discussion started by: zylwyz
5 Replies

5. Shell Programming and Scripting

Help in background process

Hi, I have a main script(main.ksh) within which I have called another script(sub.ksh). The sub.ksh script is made to run in the background using '&'. The main.ksh script logs the information in a logfile main_ddmmyy and the sub.ksh script also logs the information in the log file sub_ddmmyy.... (5 Replies)
Discussion started by: chella
5 Replies

6. UNIX for Dummies Questions & Answers

Background Process

I need to submit a script that will continue to run after logging out and after a reboot or shutdown. I entered the following: nohup script & The script continues to run in the background after logging off the system but is killed after a reboot or shutdown. Any help would be greatly... (1 Reply)
Discussion started by: powwm
1 Replies

7. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

8. AIX

Background process

how to check the process running in background? how can i run a process in background? pls explain with commands (1 Reply)
Discussion started by: udtyuvaraj
1 Replies

9. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

10. Shell Programming and Scripting

Make background process interact with fg process

Hi, I have written a menu driven shell script in which as per the choice, I run the another script on background. For eg: 1. get info 2)process info 3)modify info All the operations have different scripts which i schedule in background using &. However I wish to display the error... (0 Replies)
Discussion started by: ashima jain
0 Replies
GETTTYENT(3)						   BSD Library Functions Manual 					      GETTTYENT(3)

NAME
getttyent, getttynam, setttyent, endttyent -- get ttys file entry LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <ttyent.h> struct ttyent * getttyent(void); struct ttyent * getttynam(const char *name); int setttyent(void); int endttyent(void); DESCRIPTION
The getttyent(), and getttynam() functions each return a pointer to an object, with the following structure, containing the broken-out fields of a line from the tty description file. struct ttyent { char *ty_name; /* terminal device name */ char *ty_getty; /* command to execute, usually getty */ char *ty_type; /* terminal type for termcap */ #define TTY_ON 0x01 /* enable logins (start ty_getty program) */ #define TTY_SECURE 0x02 /* allow uid of 0 to login */ #define TTY_DIALUP 0x04 /* is a dialup tty */ #define TTY_NETWORK 0x08 /* is a network tty */ int ty_status; /* status flags */ char *ty_window; /* command to start up window manager */ char *ty_comment; /* comment field */ char *ty_group; /* tty group name */ }; The fields are as follows: ty_name The name of the character-special file. ty_getty The name of the command invoked to initialize tty line characteristics. ty_type The name of the default terminal type connected to this tty line. ty_status A mask of bit fields which indicate various actions allowed on this tty line. The possible flags are as follows: TTY_ON Enables logins TTY_SECURE Allow users with a uid of 0 to login on this terminal. TTY_DIALUP Identifies a tty as a dialin line. TTY_NETWORK Identifies a tty used for network connections. ty_window The command to execute for a window system associated with the line. ty_group A group name to which the tty belongs. If no group is specified in the ttys description file, then the tty is placed in an anonymous group called "none". ty_comment Any trailing comment field, with any leading hash marks (``#'') or whitespace removed. If any of the fields pointing to character strings are unspecified, they are returned as null pointers. The field ty_status will be zero if no flag values are specified. See ttys(5) for a more complete discussion of the meaning and usage of the fields. The getttyent() function reads the next line from the ttys file, opening the file if necessary. The setttyent() function rewinds the file if open, or opens the file if it is unopened. The endttyent() function closes any open files. The getttynam() function searches from the beginning of the file until a matching name is found (or until EOF is encountered). RETURN VALUES
The routines getttyent() and getttynam() return a null pointer on EOF or error. The setttyent() function and endttyent() return 0 on failure and 1 on success. FILES
/etc/ttys SEE ALSO
login(1), ttyslot(3), gettytab(5), termcap(5), ttys(5), getty(8), HISTORY
The getttyent(), getttynam(), setttyent(), and endttyent() functions appeared in 4.3BSD. BUGS
These functions use static data storage; if the data is needed for future use, it should be copied before any subsequent calls overwrite it. BSD
November 17, 1996 BSD
All times are GMT -4. The time now is 04:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy