LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-15-2012, 02:06 PM   #1
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Rep: Reputation: Disabled
Parallel processes execution using fork()


Hi.I have to do an exercise and i want your help.I want to create dynamicaly a number of processes using fork.The processes will be created must run in parallel and execute a common code using semaphores.Any idea?

Thank you.
 
Old 05-15-2012, 03:14 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Sure... I can help you a little with your homework!

When you use fork(), a process literally "splits itself in two." One of them perceives that the function-call returned true while the other one perceives that it returned false.

So, the first part of your assignment is to figure out how to correctly split-off n copies of yourself.

The second part of your assignment is to figure out how to cause the various split-off copies to be able to accomplish a task cooperatively, i.e. without smashing into each other because they are all trying to access the same shared data-structures (and what not...) at the same time. Here is where the idea of "semaphores" comes into play. A semaphore is like those "take a number" gadgets that you find at popular meat-counters in popular grocery stores: everybody "takes a number" so that everybody gets served, "fairly, but one-at-a-time." The mechanism allows each process to "take a number, wait patiently in line, then proceed when their 'turn' arrives."

At this point, I turn you over to the vast amount of material that is at your beck-and-call in this instantaneous worldwide research library called "the Internet." Or, as the case may be, to your instructor.

(And let me be clear on this: I have been an instructor myself. If you are confused, you're not the only one. Go to your instructor and talk to him or her. This will serve, in part, as extremely valuable feedback and guidance to him or her. Don't feel embarrassed or reticent: this process is called learning, and it's a two-way street. Do your part to expedite the process.)

Last edited by sundialsvcs; 05-15-2012 at 03:16 PM.
 
Old 05-15-2012, 03:35 PM   #3
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Thank you very much for your reply.I have made efforts to learn from my instructor but he doesn't want to help.So, i am trying to learn and do the exercise on my own.

The subject of the exercise is:
Write a program in C to produce source code without comments.Specifically,the program must read a *.c file which include c language code.After this,will be created a number of processes using fork.(it does not need to refer to the way will give the number of processes).The processes should execute parallel.Each process should remove the c language comments from the file but for specific number of characters.(it does not need to refer to the way will give the number of characters which process should edit).When all processes finished,the original(main process) process should produce the final c file without comments!

I tried to do the exercise without processes and semaphores and it's work perfect in serial mode.
In parallel?

Thank you.
 
Old 05-15-2012, 04:36 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
The exercise seems not only silly, but something that is really not suited for neither distributed processing (multiple processes) nor parallel processing (multiple threads).

In modern C (C99), there are two types of comments:
Code:
// This comment runs only to the end of the line

/* This kind of comments
 * may easily span
 * a number of lines. */
Furthermore,
Code:
const char example[] = "This /* has nothing // to do with comments.";
In other words, you cannot remove comments without parsing the entire file from start to finish. Sure, you can do something half-assed that will not work for even the trivial example above, but why teach half-assisms? The world is already full of half-arsers, we don't need more of them!

I think your instructor is lazy, stupid, or most likely, both. Get a better instructor.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parallel execution when two MPI are present chiendarret Linux - Software 0 11-26-2010 12:55 PM
LXer: Parallel SSH execution and a single shell to control them all LXer Syndicated Linux News 0 10-30-2008 01:20 PM
apache2, processes die and do not fork rjcrews Linux - Software 3 03-18-2006 03:07 PM
Run 4-processes concurrently using fork() ugp Programming 1 02-26-2006 08:49 AM
Parallel Execution Capable Scripts? irfanhab Programming 5 08-28-2004 06:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration