How to plan two tasks?

I am new to process / task management. I would like to plan two tasks. suppose

fun1() { printf("It will be printed in every 1 min \n"); } fun2() { printf("It will be printed in every 2 min \n"); } main() { fun1(); fun2(); } 

So, how to plan them to get the desired result.

I want it to run in Code :: Blocks (Windows). I want fun1 to run for 1 min and fun2 to run every 2 minutes. If I can do this in two separate processes, then tell me how I can do this. Do I need to use semaphore, mutex and all that?

+8
c windows embedded scheduled-tasks operating-system
source share
7 answers

Your example is trivial and can be planned without resorting to any planned OS or even OS synchronization services, however, in general (for non-trivial requirements) on Windows, you should use multithreading and allow the OS to perform scheduling. main() already a stream, so you only need to create another. In its simplest form:

 #include <stdio.h> #include <windows.h> DWORD WINAPI OneMinthread( LPVOID lpParam ) { for(;;) { printf("It will be printed in every 1 min \n"); Sleep(60000) ; } } int main() { CreateThread( NULL, 0, OneMinthread, 0, 0, 0) ; for(;;) { printf("It will be printed in every 2 min \n"); Sleep(120000) ; } } 

See Creating Threads for more complete thread processing in Win32. Keep in mind that the .Net framework also provides a simple class-based interface for streaming.

+3
source share

Edit: this gets support, so I would like to add clarification for posterity. This is not the best way to solve this problem - you will never want to do it manually. Consolidated user threads are nice and can be used to implement smart things like coroutines, but if you want to do this, you should use a library like libcoroutine which handles the hairy bits for you. However, although this is not a practical solution, it still presents an interesting idea and provides an interesting example of the planning and limitations of pure C99.

This is a bad answer. However, it is platform independent and, in addition, uses only the functions defined in the C99 standard.

On the other hand, it starts the CPU (there are no sleep functions in C99, so we have to wait to wait), uses what I can call magic to reserve space on the stack and completely abuse setjmp . It even uses global variables! And yet it works.

This method is called cooperative user flows, also called fibers. I applied this, as I mentioned, using setjmp and longjmp . context_switch performs simple Round Robin scheduling.

This is the code:

 #include <stdio.h> #include <setjmp.h> #include <time.h> static jmp_buf jmp[2]; static int cur; void context_switch() { /* sleep(1) */ /* C99 doesn't have any sleeping functions */ if (!setjmp(jmp[cur])) { if ((sizeof(jmp)/sizeof(*jmp)) == ++cur) cur = 0; longjmp(jmp[cur], 1); } } void fun2() { char cushion[1000]; /* reserve some stack space */ time_t old_time, new_time; cushion[0] = '@'; /* don't optimize my cushion away */ old_time = time(NULL); cur = 1; /* the first thread to context switch is this one */ setjmp(jmp[1]); while (1) { context_switch(); new_time = time(NULL); if ((new_time - old_time) > (2 * 60)) { old_time = new_time; printf("Printed every 2 minutes\n"); } } } void fun1() { char cushion[1000]; /* reserve some stack space */ time_t old_time, new_time; cushion[0] = '@'; /* don't optimize my cushion away */ if (!setjmp(jmp[0])) fun2(); old_time = time(NULL); while (1) { context_switch(); new_time = time(NULL); if ((new_time - old_time) > (1 * 60)) { old_time = new_time; printf("Printed every 1 minute\n"); } } } int main(int argc, char **argv) { fun1(); return 0; } 

And this is the result that I get:

 $ gcc -ggdb -std=c99 -o silly silly_setjmp.c $ ./silly Printed every 1 minute Printed every 2 minutes Printed every 1 minute Printed every 1 minute ... 
+15
source share

Well, it would be better if you could specify your operating system (or cross-platform requirement)

Yo can write:

  • OS dependent code
  • Cross-platform code (running on multiple operating systems)

For multitasking, each of the above can use:

  • Themes or
  • The processes
  • Timers

illogical. POSIX compatible OS (like Linux), using processes

 void fun1() { for(;;) { printf("It will be printed in every 1 min \n"); sleep(60); } } void fun2() { for(;;) { printf("It will be printed in every 2 min \n"); sleep(2*60); } } int main() { pid_t pID = fork(); if ( 0 == pID ) // new, child process { func1(); } else if(pID<0) { printf("Fork failed 1\n"); } else //parent process succeeded forking and now continue running { func2(); } return 0; } 

Other cases:

  • POSIX (Linux / UNIX) + threads: use the pthread_create function to create threads
  • Windows + threads / processes: use the CreateThread () or CreateProcess () function
  • Crossplatform: Use special high-level libraries such as GLIB to create threads / processes.
+3
source share

Next, two threads are created. Topic number 1 prints once a minute, and thread number 2 prints once every 2 minutes. These threads will be scheduled by your OS scheduler. On Linux, we have cfs to do the planning. For an overview of planning, read this.

 #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NOTHREADS 2 void * fun1(void *thread_id) { int i; int *id = (int *) thread_id; while(1) { usleep(1000 * 1000 * 60); printf("1 minute \n"); } pthread_exit(NULL); } void * fun2(void *thread_id) { int i; int *id = (int *) thread_id; while(1) { usleep(2000 * 1000 * 60); printf("2 minute \n"); } pthread_exit(NULL); } int main() { pthread_t tids[NOTHREADS]; int ids[NOTHREADS] = {1, 2}; int ret; long t; int i; printf("Creating fun1 thread \n"); ret = pthread_create(&tids[0], NULL, fun1, &ids[0]); if (ret) { printf("unable to create thread! \n"); exit(-1); } printf("Creating fun2 thread \n"); ret = pthread_create(&tids[1], NULL, fun2, &ids[1]); if (ret) { printf("unable to create thread! \n"); exit(-1); } for (i=0 ; i<NOTHREADS; i++) { pthread_join(tids[i], NULL); } pthread_exit(NULL); return 0; } 

exit:

 $ gcc tc -lpthread $ ./a.out Creating fun1 thread Creating fun2 thread 1 minute 2 minute 1 minute 1 minute ^C $ 

Hope this helps!

+2
source share

The easiest, although inaccurate, way to do this is to use the POSIX sleep() function inside an infinite loop.

 while(1) { fun1(); sleep(60); fun1(); fun2(); sleep(60); } 

If you have more complex tasks to implement, you might be interested in POSIX threads and POSIX timers.

+1
source share

This approach is performed using setjmp and longjmp operations using the sleep command.

 #include<stdio.h> #include<setjmp.h> main() { jmp_buf env; int i; i=setjmp(env); if(i==1) { sleep(1); printf("It will be printed in every 1 min \n"); longjmp(env,3); } else if(i==2){ printf("It will be printed in every 2 min \n"); longjmp(env,1); } else if(i==3) { sleep(1); printf("It will be printed in every 1 min \n"); longjmp(env,2); } longjmp(env,1); } 
+1
source share

I think you should use a scheduling algorithm like Round-Robin, or create your own algorithm.
Here you will find some algorithms http://www.centos.org/docs/5/html/5.1/Virtual_Server_Administration/s2-lvs-sched-VSA.html
You can find examples of how they are implemented and which to use.

0
source share

All Articles