Exemple #1
0
int main()
{
  if(my_fork()) { 
    printf("I'm parent: My PID=%d\n",my_getpid());
  }
  else { 
    printf("I'm child: My PID=%d and my parent %d\n",
	   my_getpid(), my_getppid());
  }
}
Exemple #2
0
int treeTest(){
    int i,j,k,total,s1,s2,count,t;
    char str[1024],dir[1024];
    struct dirent **namelist;
    strcpy(dir,default_path);
    total = scandir(dir, &namelist, 0, alphasort);
    printf("path=%s,total=%d\n",dir,total);
    for(i=0;i<total;i++){
        strcpy(str,namelist[i]->d_name);
        if(str[0]>='0'&&str[0]<='9')
            count++;
    }
    printf("进程数:%d\n",count);
    info file[1024];
    i=0;t=0;
    while(i<total){
        FILE *fp;
        char path[1024],name[1024];
        int pid,ppid;
        strcpy(str,namelist[i]->d_name);
        strcpy(path,default_path);
        //printf("%s\n",str);
        if(str[0]>='0'&&str[0]<='9'){
            strcat(path,str);
            strcat(path,"/status");
            fp=fopen(path,"r");
            while(!feof(fp)){
                fgets(str,1024,fp);
//pid
                if((s1=my_getpid(str))!=0)
                    pid=s1;
//ppid
                if((s2=my_getppid(str))!=0)
                    ppid=s2;
//name
                strcpy(file[t].name,my_getName(str));

                file[t].pid=pid;
                file[t].ppid=ppid;

            }
            fclose(fp);
            printf("%d,%s,pid=%d,ppid=%d,name=%s#\n",t,path,file[t].pid,file[t].ppid,file[t].name);
            t++;
        }
        i++;
    }
    memset(&file->flag,0,count);
    memset(&file->rec,0,count);
    print_pstree(file,count,0,0);
    while (1){;}
}
Exemple #3
0
static int			init_mysh(t_term *s_term, char **environ)
{
	char			*env_path;

	my_bzero((void *)s_term, sizeof(*s_term));
	if (environ && *environ)
	{
		get_environ(s_term, environ);
		static_term(s_term);
		init_signals();
		s_term->tsave = init_termcap(s_term->environ);
		s_term->pid = my_getpid();
		s_term->progs = NULL;
		if ((env_path = my_getenv(environ, "PATH="))
			&& (s_term->progs = get_path_progs(env_path))
			&& !add_builtins(s_term->progs))
			return (1);

	}
	return (0);
}
Exemple #4
0
/** Processes command line arguments using argtable library
 <p>To add a smafeopt class backed command line argument,
 perform the following steps:
 <ol>
 <li>Add public member in smafeopt.h			(smafe)
 <li>Assign default value in smafeopt.cpp	(smafe)
 <li>Define a new struct (arg_lit, arg_int etc) in this method
 (processCommandLineArguments()) and add details  (shortopts, longopts)
 according to appropriate constructor
 <li>Add this struct to void* argtable[] array
 <li>Transfer the value from the struct to the smafeopt instance
 (<i>if (nerrors == 0)</i> block)
 </ol>
 * @param argc number of command line arguments (from main())
 * @param argv array of c-strings (from main())
 * @param so initialized, empty  smafeopt instance that is to be filled
 */
void processCommandLineArguments(int argc, char* argv[]) {

	/* Define the allowable command line options, collecting them in argtable[] */
	/* Syntax 1: command line arguments and file or dir */
	// daemon options
	struct arg_str
	*arg_daemonID =
			arg_str0(
					NULL,
					"id",
					"IDENTIFIER",
					"Identifier for daemon instance. IDENTIFIER must not contain whitespace or special chars.");
	struct arg_int *arg_interval = arg_int0(NULL, "interval", "MINUTES",
			"Polling interval in minutes. Default is 10");
	struct arg_lit
	*arg_no_daemon =
			arg_lit0(NULL, "no-daemon",
					"Runs program as normal forground process (no forking). No logfile allowed");
	struct arg_str *arg_log = arg_str0(NULL, "log", "FILENAME",
			"Name of logfile. If not specified, a random name is chosen.");
	struct arg_lit *arg_stats = arg_lit0(NULL, "stats",
			"Print number of open tasks and exit");


	//struct arg_str *arg_passphrase = arg_str0("p", "passphrase", "PASSPHRASE", "Passphrase for database encryption (max 63 characters)");
	struct arg_int
	*arg_verbose =
			arg_int0(
					"v",
					"verbosity",
					"0-6",
					"Set verbosity level (=log level). The lower the value the more verbose the program behaves. Default is 3");

	struct arg_int *arg_lFvtId = arg_int1("f", "featurevectorype_id",
			"FEATUREVECTORTYPE_ID",
			"Featurevectortype_id to use. Must be contained in the database.");
	struct arg_str *arg_dbconf = arg_str0(NULL, "dbconf", "DATABASE-CONFIGURATION-FILE",
			"Specify file that contains database connection details");
	struct arg_lit *help = arg_lit0(NULL, "help", "print this help and exit");
	struct arg_end *end = arg_end(20);

	void* argtable[] = { arg_daemonID, arg_lFvtId,
			arg_no_daemon, arg_interval, arg_dbconf, arg_verbose, arg_log,
			arg_stats, help, end };

	int nerrors;

	/* verify the argtable[] entries were allocated sucessfully */
	if (arg_nullcheck(argtable) != 0) {
		/* NULL entries were detected, some allocations must have failed */
		std::cerr << PROGNAME << ": insufficient memory" << std::endl;
		exit(2);
	}

	// if no parameter is given: show help
	if (argc > 1) {
		// Parse the command line as defined by argtable[]
		nerrors = arg_parse(argc, argv, argtable);
	} else {
		// no argument given
		help->count = 1;
	}

	/* special case: '--help' takes precedence over error reporting */
	if (help->count > 0) {
		std::cout << "Usage: " << PROGNAME;
		arg_print_syntax(stdout, argtable, "\n");
		std::cout << std::endl;
		arg_print_glossary(stdout, argtable, "  %-27s %s\n");
		std::cout << "" << std::endl;

#if defined(SMAFEDISTD_REAL_DAEMON)
		std::cout
		<< "This program works as a daemon, i.e., it is executed in the background and not attached to a shell."
		<< std::endl;
		std::cout << std::endl;
		std::cout
		<< "Currently, the preferred way to stop a running daemon is sending a SIGTERM signal to it. Then, the program will exit after finishing the current job (if any)."
		<< std::endl;
		std::cout << std::endl;
		std::cout << "How to send a SIGTERM signal to a running instance:"
				<< std::endl;
		std::cout << "  1) ps aux | grep " << PROGNAME
				<< "          (this gives you the <PID>)" << std::endl;
		std::cout << "  2) kill <PID>" << std::endl;
#else
		std::cout << "This program works NOT as a daemon because this platform does not support forking (or, there has been a problem at compiling the application)." << std::endl;
#endif
		exit(1);
	}

	if (nerrors == 0) {
		// verbosity level
		// must be on top
		if (arg_verbose->count > 0) {
			loglevel_requested = arg_verbose->ival[0];
			// change loglevel
			SmafeLogger::smlog->setLoglevel(loglevel_requested);
		} else
			loglevel_requested = SmafeLogger::DEFAULT_LOGLEVEL;

		// identifier
		if (arg_daemonID->count > 0) {
			daemonId = std::string(arg_daemonID->sval[0]);
			if (daemonId == SmafeStoreDB::STATUSOK || (0 == daemonId.find(SmafeStoreDB::STATUSFAILED))) {
				SMAFELOG_FUNC(SMAFELOG_FATAL, "Identifier " + daemonId + " is illegal. It must not be '"+SmafeStoreDB::STATUSOK+"' and must not start with '"+SmafeStoreDB::STATUSFAILED+"'");
				exit(2);
			}
		} else {
			if (arg_stats->count == 0) {
				// is actually mandatory, only for --stats and --list it is not.
				SMAFELOG_FUNC(SMAFELOG_FATAL, "Please specify an identifier for this daemon (--id).");
				exit(2);
			}
		}

		// logfile
		// uses daemonId
		if (arg_log->count > 0) {
			sLogfilename = std::string(arg_log->sval[0]);
		} else {
			sLogfilename = std::string(PROGNAME) + "." + stringify(my_getpid())
																			+ ".log";
		}

		// no-daemon
		if (arg_no_daemon->count > 0) {
#if defined(SMAFEDISTD_REAL_DAEMON)
			if (arg_log->count > 0) {
				SMAFELOG_FUNC(SMAFELOG_FATAL, "--no-daemon and --log cannot be combined. If program runs as forground process output is written to stdout and stderr.");
				exit(1);
			} else {
				// no daemon
				SMAFELOG_FUNC(SMAFELOG_INFO, "Running in 'normal mode' (ie, not as daemon)");
				bNoDaemon = true;
			}
#else
			SMAFELOG_FUNC(SMAFELOG_INFO, "Parameter --no-daemon is implied since this executable is compiled without daemon mode support.");
#endif
		}

		// stats
		if (arg_stats->count > 0) {
			bPrintStatsOnly = true;
#if defined(SMAFEDISTD_REAL_DAEMON)
			SMAFELOG_FUNC(SMAFELOG_INFO, "Stats mode, so running in 'normal mode' (ie, not as daemon)");
			bNoDaemon = true;
#endif
		} else
			bPrintStatsOnly = false;



		// polling interval
		if (arg_interval->count > 0)
			pollInterval = arg_interval->ival[0];
		if (pollInterval == 0) {
			SMAFELOG_FUNC(SMAFELOG_WARNING, "Polling interval set to 0 which means that the daemon will perform busy waiting.");
		}
		if (pollInterval < 0) {
			SMAFELOG_FUNC(SMAFELOG_INFO, "Daemon will stop after last finished task (since pollInterval < 0)");
		} else {
			SMAFELOG_FUNC(SMAFELOG_INFO, "polling interval=" + stringify(pollInterval));
		}

		// fvtid
		if (arg_lFvtId->count > 0)
			lFvtId = arg_lFvtId->ival[0];
		else {
			SMAFELOG_FUNC(SMAFELOG_FATAL, "Specify fvtype_id");
			exit(2);
		}
		if (lFvtId < 0) {
			SMAFELOG_FUNC(SMAFELOG_FATAL, "Featurevectortype_id cannot be < 0");
			exit(2);
		}

		// ---- db stuff
		// db options file
		if (arg_dbconf->count > 0) {
			SMAFELOG_FUNC(SMAFELOG_DEBUG, "Parsing db configuration file");
			so->parseDbOpts(std::string(arg_dbconf->sval[0]));
		}
		/*
		// Passphrase
		if (arg_passphrase->count > 0) {
			if (strlen(arg_passphrase->sval[0]) <= 63) {
				strcpy(verysecretpassphrase, arg_passphrase->sval[0]);
				SMAFELOG_FUNC(SMAFELOG_INFO, "Data encryption / decryption is enabled.");
			} else {
				SMAFELOG_FUNC(SMAFELOG_FATAL, "Passphrase too long. Max 63 characters.");
				exit(2);
			}
		} else {
			SMAFELOG_FUNC(SMAFELOG_INFO, "Data encryption / decryption is DISABLED!");
		}
		 */

		// switch to logfile was here

	} else {
		arg_print_errors(stdout, end, PROGNAME);
		std::cout << "--help gives usage information" << std::endl;
		exit(1);
	}
	arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
}