Example #1
0
void * worker(void * arg)
{
    t_threadarg * info = (t_threadarg *) arg;
    int status = info->threadinfo->status;

    writelog(LOG_DEFAULT, "Starting Worker-Thread (Thread: %d)", info->threadinfo->id);

    t_job * job;
    while (1) {

        while((job = jobfind(info->supervisor, info->threadinfo)) != NULL) {
            info->threadinfo->status = status = THREAD_BUSY;
            writelog(LOG_DEBUG, "Start processing %s (Thread: %d)", job->file, info->threadinfo->id);

            job->status = JOB_PROCESSING;
            job->content = readfile(job);

            /**
             * Send data to Post endpoint
             */
            if(job->content != NULL) {
                if(senddata(job)) {
                    writelog(LOG_ERROR, "Resetting Job-Status for %s (Thread: %d)", job->file, info->threadinfo->id);
                    job->status = JOB_READY;
                    continue;
                } else {
                    writelog(LOG_DEFAULT, "Finished Processing %s (Thread: %d)", job->file, info->threadinfo->id);
                }
            }

            jobfinish(info->supervisor, job);

            sleep(1);
        };

        status = THREAD_IDLE;

        if(info->threadinfo->status != status) {
            info->threadinfo->status = status;
            writelog(LOG_DEFAULT, "Waiting for Jobs (Thread: %d)", info->threadinfo->id);
        }

        sleep(1);
    }

    return 0;
}
Example #2
0
/* eveccommand
 * Function takes a parsed character array, and exec's it through a
 * child process.
 */
void eveccommand(char * argv[MAXARGV], int bg)
{
	pid_t pid;		//new process id
   sigset_t mask;
	int argc = 0;
	int saved_stdout = dup(1);
	FILE * o_file = NULL;

	if (argv[0] == NULL)	//No command, so return without exec
		return;

	//Block child signals
	sigemptyset(&mask);
	sigaddset(&mask, SIGCHLD);
	sigprocmask(SIG_BLOCK, &mask, NULL);

	while(argv[argc])
		++argc;

	//If >, then open output file, and change file descriptor for
	//stdout to this file
	if(argc > 2 && !strcmp(argv[argc - 2], ">"))
	{
		o_file = freopen(argv[argc-1], "w", stdout);

		if(!o_file)
			fprintf(stderr, "Failed to run command '%s': %s\n",
			argv[0], strerror(errno));

		argv[argc-2] = NULL; //stop exec from getting using ">"
	}

	//Child process
	if ((pid = Fork()) == 0)
	{
		//unblock SIGCHLD
		sigprocmask(SIG_UNBLOCK, &mask, NULL);

		if (execvp(argv[0], argv) < 0)
		{
			fprintf(stderr, "Failed to run command '%s': %s\n",
			argv[0], strerror(errno));

			exit(1);
		}
	}

	//Parent waits if not a background process
	if(!bg)
	{
		jobadd(pid);

		//Now that child pid is in the job list, unblock SIGCHLD signals
		sigprocmask(SIG_UNBLOCK, &mask, NULL);

		//Until child process is not on the job list, sleep 100 microseconds
		while(jobfind(pid))
			usleep(100);

		//Put output back to stdout, if needed
		dup2(saved_stdout, 1);
	}
	else
	{
		jobadd(pid);

		//Put output back to stdout, if needed
		dup2(saved_stdout, 1);

		printf("Background: (%d) %s\n", pid, argv[0]);
	}

	return;
}