LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-09-2010, 07:56 PM   #1
Sabre717
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
Question Worker Program Help


Here's what I'm trying to do: I'm attempting to create a Worker program which is to be called in a Coordinator program. The worker program needs to add two numbers from the argv and return the sum of those numbers. Basically my problem is some sort of syntax and I'm fairly new to this scripting stuff. Here's what I have:

#!/bin/bash
# Worker class for the program Coordinator

int worker(int argc, char *argv[]
{
int sum = 0;
sum = atoi(argv[1]) + atoi(argv[2]);
printf("I am child with PID %d", getpid());
printf("sum=%d\n", sum);
fprintf(atoi (argv[3]), sum, 4);
exit(sum);
}

Here's the errors:

~$./worker 2 4
./worker: line 4: syntax error near unexpected token `('
./worker: line 4: `int worker(int argc, char *argv[]'
 
Old 03-09-2010, 08:07 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
why c in bash script?
Code:
#!/bin/bash
echo $(($1+$2))
should be enough

Last edited by troop; 03-09-2010 at 08:09 PM.
 
Old 03-09-2010, 08:16 PM   #3
Sabre717
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
I'm trying to learn how forking works. What I'm gonna do is write the Coordinator program, which will fork new worker processes which will return their sums to new fork processes which will sum their sums..etc

say input is 5 3 2 3

worker1 would sum 5+3, return 8
worker2 would sum 2+3, return 5
worker3 would sum 8+5, return 13

thus the coordinator has to do no additions..
 
Old 03-09-2010, 08:50 PM   #4
Sabre717
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Updated code, still same error:

#!/bin/bash -x
# Worker class for the program Coordinator
#include<stdio.h>
#include <sys/types.h>

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

{
int sum = 0;
sum = atoi(argv [1]) + atoi(argv [2]);
printf("I am child with PID %d", getpid());
printf("sum=%d\n", sum);
fprintf(atoi(argv [3]), sum, 4);
exit(sum);
}
}

error:
~$ ./worker 2 4
./worker: line 6: syntax error near unexpected token `('
./worker: line 6: `int main() '
 
Old 03-10-2010, 01:36 AM   #5
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
it's steel c inside of bash. bash and c are completely different things. If use c then create worker.c file and compile one.

Last edited by troop; 03-10-2010 at 01:38 AM.
 
Old 03-10-2010, 12:21 PM   #6
Sabre717
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Your right. I feel like a huge tard.
 
  


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
apache MPM prefork or worker mario.almeida Linux - Server 2 11-25-2009 11:45 PM
My co worker is driving me crazy. DotHQ General 15 03-23-2008 01:53 PM
how can i tell if apache 2 is prefork or worker? sneakyimp Linux - Software 4 09-15-2007 02:15 PM
Apache2 worker works, prefork doesn't AlexH SUSE / openSUSE 1 10-18-2004 11:15 AM
what if I use worker and prefork on apache? gottin Linux - Software 1 10-15-2004 03:19 PM

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

All times are GMT -5. The time now is 06:34 AM.

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