11 Replies - 287 Views - Last Post: 27 June 2013 - 05:56 AM Rate Topic: -----

#1 robgeek  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 15-January 13

Task scheduler in c doesn't work properly.

Posted 22 June 2013 - 07:19 PM

Hello, guys.
I have to make a task scheduler in c in which a process called task_scheduler should be run in the terminal command using the following syntax:
task_scheduler <hh:mm> <n> <task_name>
So, here is what i did until now. I created a file called task_scheduler.c:
#include "scanner.h"//Validates the format of the parameters coming from the terminal. Works fine.
#include "tasks.h"//Linked list where tasks are scheduled. Woks fine.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) {

	List *list = NULL;
	int status;
	daemon(1, 1);//To leave the terminal free so i can use to schedule a new task, but it doesn't work properly.
		//Continue if there are four arguments coming from terminal except the first.
		if( (argc-1) == 3 ) {
			if( timer( argv[1] ) && amount( argv[2] ) ) {
				printf("Parameters accepted.\n");
				list = addTask( list, argv[1], argv[2], argv[3] );
			}
		}
		else {
			printf("Parameters not accepted.\n");
			return 1;
		}

	return 0;
}

Everything works fine in this code, but i can add only one task! If i enter in terminal:
./task_scheduler 12:12 34 anything
./task_scheduler 23:34 56 lolololol
only one task is added! The second task is added in a new instance of task_scheduler and this is not what i want!

How can i solve this problem?

Is This A Good Question/Topic? 0
  • +

Replies To: Task scheduler in c doesn't work properly.

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Task scheduler in c doesn't work properly.

Posted 23 June 2013 - 12:33 AM

Since each time you run the program, the running process will be given it's own separate memory, each process will have a separate instance of the task manager. You could use interpocess communication and keep a single class instance in shared memory between the processes (boost::interprocess).

Another option is to keep a single instance of the process running and use a socket to send the daemon task information.

Lastly, you could create a pipe to send information to the daemon.

This post has been edited by jjl: 23 June 2013 - 12:34 AM

Was This Post Helpful? 1
  • +
  • -

#3 robgeek  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 15-January 13

Re: Task scheduler in c doesn't work properly.

Posted 23 June 2013 - 06:58 PM

Quote

Since each time you run the program, the running process will be given it's own separate memory, each process will have a separate instance of the task manager. You could use interpocess communication and keep a single class instance in shared memory between the processes (boost::interprocess).

Another option is to keep a single instance of the process running and use a socket to send the daemon task information.

Lastly, you could create a pipe to send information to the daemon.


Sorry I did not understand.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Task scheduler in c doesn't work properly.

Posted 23 June 2013 - 10:06 PM

Quote

Sorry I did not understand

Which part?
Was This Post Helpful? 1
  • +
  • -

#5 robgeek  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 15-January 13

Re: Task scheduler in c doesn't work properly.

Posted 24 June 2013 - 06:16 AM

How can i get access of the address of my first process only with his pid to send him a node of my structure?
./task_ 12:34 34 laoalsl -> Has a address and pid, like, 1234
./task_ 12:34 34 laoalsl -> Has a different address of the first process and pid, like, 1235.

For example, in the second call system, in the code, i have to check if there is a pid smaller than his own, in other words, if there is a pid equal to 1234, then he send his node to the process which pid is 1234. How? I have no idea!
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: Task scheduler in c doesn't work properly.

Posted 24 June 2013 - 06:41 AM

Why do you need to get the PID of the other process, when you can just share a common key if you are going to use shared memory?
http://linux.die.net/man/2/shmget

Or use a shared port number if you are going to use a socket.

Or use a shared pipe name if you are going to use pipes.
Was This Post Helpful? 1
  • +
  • -

#7 robgeek  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 15-January 13

Re: Task scheduler in c doesn't work properly.

Posted 24 June 2013 - 08:22 AM

Quote

Why do you need to get the PID of the other process, when you can just share a common key if you are going to use shared memory?
http://linux.die.net/man/2/shmget
Or use a shared port number if you are going to use a socket.
Or use a shared pipe name if you are going to use pipes.

Mmmm, ok, i'll use pipe. I need shmget to use a shared pipe?
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Task scheduler in c doesn't work properly.

Posted 24 June 2013 - 01:52 PM

I would go with message passing model via sockets to send tasks to the scheduler. Create a program where it's sole purpose is to wait for incoming task packets (TCP or UDP packets) - this process will run as a daemon. Now create a seperate program that it will take the command line arguments and build a packet and send it to the scheduler dameon process.

i.e.
task scheduler pseduo code
create_socket
initialize_socket
while read_packet_from_socket
   build_task_from_packet
   schedule_task
close_socket



add task pseduo code
create_socket
initialize_socket
build_task_packet_from_args
send_packet
close_socket


This post has been edited by jjl: 24 June 2013 - 01:53 PM

Was This Post Helpful? 1
  • +
  • -

#9 robgeek  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 15-January 13

Re: Task scheduler in c doesn't work properly.

Posted 24 June 2013 - 05:12 PM

Quote

I would go with message passing model via sockets to send tasks to the scheduler.


Please, explain to me why you made this choice in place of pipes?

I'll try you way, using sockets.
Was This Post Helpful? 0
  • +
  • -

#10 robgeek  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 15-January 13

Re: Task scheduler in c doesn't work properly.

Posted 24 June 2013 - 06:03 PM

And one more thing. When you say.

Quote

Create a program where it's sole purpose is to wait for incoming task packets (TCP or UDP packets) - this process will run as a daemon.

This program i have to run manually, right? Like:
./daemonprogram
./task_ 12:34 34 laoalsl
./task_ 12:34 34 laoalsl
...
...

There is a way to run him automatically but just one time? The only thing i would like to enter into terminal is
"./task_ 12:34 34 laoalsl"

Thank you for your answer.
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Task scheduler in c doesn't work properly.

Posted 25 June 2013 - 12:43 PM

I chose sockets just because that is my preference, pipes & files are also a viable solution as well.

Quote

There is a way to run him automatically but just one time? The only thing i would like to enter into terminal is


If you are going to use pipes and files, you can determine if the task scheduler is running if the file used for queuing pipe messages is open. If the task file is not open, then fork and fire off scheduler.

Maybe this will give you some ideas. If you create a program that polls a binary file for data, you can have another process write task information to the file.

i.e. (scheduler)
int main() {
   task_scheduler sched;
   task tsk;
   unsigned bytes;
   unsigned curr;
   int file = open(".taskqueue", O_CREAT | O_RDONLY, S_IREAD | S_IWRITE);
   
   if(file < 0) {
      fprintf(stderr, "%s", strerror(errno));
      return EXIT_FAILURE;
   }
   /* get current file potition */
   curr = lseek(file, 0, SEEK_CUR);

   /* loop forever */
   for(;;)/>/>/> {
      /* read task from file */
      bytes = read(file, (void*)&tsk, sizeof(task));

      /* if the task read was not complete (maybe the add process is still writing it) */
      if(bytes != sizeof(task)) {
         /* jump back to where you were and try again on the next iteration */
         lseek(file, curr, SEEK_SET);
      }
      else {
         /* task was read successfuly, schedule it */
         schedule_task(sched, tsk);
         curr += bytes;
      }
      usleep(10000); /* sleep for 10 ms since were just polling for data */
   }
   close(file);



Using a file method makes it easier to add tasks to the scheduler processes since the file is named specifically and any process can write to it. A strict pipe is normally used when there is a parent heiarchy involved, where the parent can set up and copy file descriptors for the child it is about to spawn off.

This post has been edited by jjl: 25 June 2013 - 12:48 PM

Was This Post Helpful? 1
  • +
  • -

#12 robgeek  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 15-January 13

Re: Task scheduler in c doesn't work properly.

Posted 27 June 2013 - 05:56 AM

Thanks for your help, guys! I'll use socket to solve my problem. I already started.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1