![]() |
| | #1 |
| System.out.println(""); Join Date: Jan 2005
Posts: 84
| Creating makefiles Code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
void childProcess(int currentProcess);
void parentProcess(int numProcesses);
int atoi( const char *str );
int numLines; //number of lines in each file
int numProcesses; //number of processes entered by the user
int numLinesPerFile;
pid_t pid; //process ID so we know whether it's a child or parent processs
int status;
char fileNames[] = "abcdefghijklmnopqrstuvwxyz"; //this program can handle up to 26 processes
int main(int argc, char *argv[])
{
//get the information from the command line
numLines = atoi(argv[2]);
numProcesses = atoi(argv[3]);
numLinesPerFile = numLines / numProcesses;
//check to make sure the user entered the correct number of arguments. If they didn't,
//print an error message and quit
if(argc != 4)
{
printf("\nERROR! Wrong number of arguments.\n\n");
return 0;
}
//print out the parameters the user entered for verification
//this prints out the filename entered
fprintf(stdout,"\n");
fprintf(stdout, argv[1]);
printf("\nNumber of lines: %d\n", numLines);
printf("Number of processes: %d\n", numProcesses);
printf("\n\nNumber of lines per file: %d\n", numLinesPerFile);
//arbitrary buffer size of 200 to be sure that the string will fit
//this will split the file up according to the number of processes
char buff[200];
sprintf(buff, "split -l %d %s ", numLinesPerFile, argv[1]);
system(buff);
int extraLines = numLines - numLinesPerFile*numProcesses;
//printf("\n\nThe number of extra lines is: %d", extraLines);
if(extraLines != 0)
{
char buff[200];
sprintf(buff, "sort -o xa%c -m xa%c xa%c", fileNames[numProcesses-1], fileNames[numProcesses-1], fileNames[numProcesses] );
system(buff);
sprintf(buff, "rm xa%c", fileNames[numProcesses]);
system(buff);
}
//this portion of the code will use the fork() to create new processes
int i;
for(i = 0; i < numProcesses; i++)
{
//keep up with the current process so we know which file to sort
int currentProcess = i;
//fork the current process
pid = fork();
if(pid == 0) //i am a child!
{
childProcess(currentProcess);
}
else //i am your parent!
{
//pass the number of processes so the parent knows how many files to merge
parentProcess(numProcesses);
}
}
return 0;
}//end of main function
void childProcess(int currentProcess)
{
//this portion will sort the file of the child we are on
char buff[200];
sprintf(buff, "sort xa%c -n -o xa%c", fileNames[currentProcess], fileNames[currentProcess]);
system(buff);
return;
}//end child function
void parentProcess(int numProcesses)
{
printf("\nI AM YOUR FATHER!\n");
int l;
//for(l = 0; l < numProcesses; l++)
//{
//wait for all of the children to catch up
wait(&status);
//}
int j;
//arbitrary buffer sizeto hold the entire merge
char buffer[2000];
for(j = 0; j < numProcesses; j++)
{
//if we are on the first item just take the name xaa
if(j == 0)
{
sprintf(buffer, "xa%c ", fileNames[j]);
}
//if we aren't on the first file we have to do some concatenation with
//the new file name and the old ones
else
{
char newBuffer[200];
sprintf(newBuffer, "xa%c ", fileNames[j]);
strcat(buffer, newBuffer);
}
}
//this part actually merges the sorted children's files
char string[20000];
sprintf(string, "sort -o outfile -m -n %s", buffer);
system(string);
//remove all of the intermediate files
//sprintf(string, "rm %s", buffer);
//system(string);
return;
}//end parent function
|
| Loctan is offline | |
| | #2 |
| System.out.println(""); Join Date: Jan 2005
Posts: 84
| I found the answer. |
| Loctan is offline | |
| | #3 | |
| Registered User Join Date: Feb 2005
Posts: 416
| good. now, just an idea. post the answer so we have a refference next time someone hits the same problem.
__________________ Quote:
| |
| Jaqui is offline | |
| | #4 |
| System.out.println(""); Join Date: Jan 2005
Posts: 84
| Suppose your program is named Beginning.c Code: P1: Beginning.o gcc Beginning.o -o P1 Beginning.o:Beginning.c gcc -c Beginning.c Then type ./P1 to execute your program. |
| Loctan is offline | |
| | #5 | |
| Registered User Join Date: Feb 2005
Posts: 416
| and if beginning.c needed to link to an .so, which your makefile had to create? ( or call the makefile to create it ) [ most likely one of the lessons in the class ] ![]() if your app's main file required other files to be compiled into .obj, and it required linking to a dynamic loading library, what would you need to add to existing makefile to get the executable? ( being nasty and asking for more advanced skills, only to push you into looking at a more realistic model of a makefile )
__________________ Quote:
| |
| Jaqui is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Profiler Valgrind | afflictedd2 | C++ Programming | 4 | 07-18-2008 09:38 AM |
| creating an application | Cpro | C++ Programming | 21 | 05-30-2008 12:21 AM |
| Creating Custom Controls | Grayson_Peddie | C# Programming | 1 | 12-29-2003 01:41 PM |
| programming linux with gcc, emacs, and makefiles | Captain Penguin | Linux Programming | 1 | 11-02-2002 12:04 PM |
| problems creating a linked list | jamjar | C Programming | 5 | 10-23-2002 05:50 AM |