Having strange behaving programs,at the bottom you can find them, but here is short explenation.

Main program will present a menu from which you can pick operation to do. When I choose 1 it is suppouse to create file of certain name(not exactly, you have to complile score.c program and on command line pass name of file which you wish to create, I'm using "score") with new 5 accounts of zero value, dispalay its inode number and syze than get back to main program.
What is does is :
- display option menu and ask for number of operation to perform
- display again menu
- its displays info about new accounts and hang, thats how is it looks like

here is output

* * * * * * * * * * * * * * * * * * * * * * * *
1. New Game
2. Display current score
3. Enter new score
4. Exit game
* * * * * * * * * * * * * * * * * * * * * * * *

Choose number for operation to perform : 1
* * * * * * * * * * * * * * * * * * * * * * * *
1. New Game
2. Display current score
3. Enter new score
4. Exit game
* * * * * * * * * * * * * * * * * * * * * * * *

Choose number for operation to perform : 0
0
0
0
0
Inumber :        7550163
Size in bytes :  20

However that is not true, after above discribed actions is actualy waiting for new number for operation. So it is just matter of processing.
Question
How can I force program to display information in correct order!!!
MENU ->if 1 -> CREATE NEW FILE -> NEW MENU

MAIN program

//MAIN PROGRAM

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

void newGame()
{
  if(fork()==0)
    {
    execl("score.out","score.out","score",NULL);
    printf("\nback to newGame\n");
    }
}

main ( int argc, char *argv[])
{
  int fd, i, status;

do
  {
    printf("* * * * * * * * * * * * * * * * * * * * * * * *\n");
    printf("1. New Game\n");
    printf("2. Display current score\n");
    printf("3. Enter new score\n");
    printf("4. Exit game\n");
    printf("* * * * * * * * * * * * * * * * * * * * * * * *");
    printf("\n\nChoose number for operation to perform : ");
    scanf("%d",&status);

    switch(status){
    case 1: newGame();
      break;
    case 2: printf("choosen display score\n");
      break;
    case 3: printf("choosen enter new value\n");
      break;
    default:
      break;
    }
  }while( status != 4);
}

SCORE.C

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

main( int argc, char *argv[])
{
  struct stat buf;
  int fd, i;
  int a=0;

  fd = open(argv[1], O_RDWR | O_CREAT, 0600);
  if (fd<0) {
    perror("cannot open file");
    exit(1);
  }

  for(i=0; i < 5; i++)
    {
      write(fd,&a, sizeof (int));
      printf("%d\n", a);
    }

  close(fd);


  if(stat(argv[1], &buf) !=0)
    {
      perror(argv[1]);
      exit(2);
    }

  printf("Inumber :        %d\n", buf.st_ino);
  printf("Size in bytes :  %d\n", buf.st_size);
}

Recommended Answers

All 3 Replies

Hmm, was not really sure what you were doing with exec, but I suppose you compile score.c to score.out... you never return from exec btw so what you want would probably be (and do check bad returns plz)

pid_t pid ;
if( (pid = fork()) == 0 )
{
  execl("score.out","score.out","score",NULL);
  perror( "Im fucked" ) ;
} 
wait( pid ) ; // Not sure about this one, might need status variable, check man
puts("Game ended") ;

And I have not really checked the rest of the code for any sanity, fix that and se what the results are...

hmm, this sorted my problem with print order, but come new problem.
This how newGame function in main program looks like

void newGame()
{
  int cid;
  if((cid = fork())==0)
    {
    execl("score.out","score.out","score",NULL);
    perror("Didn't work.");
    }
  wait( cid);
}

Here is copy of output

* * * * * * * * * * * * * * * * * * * * * * * *
1. New Game
2. Display current score
3. Enter new score
4. Exit game
* * * * * * * * * * * * * * * * * * * * * * * *

Choose number for operation to perform : 1
0
0
0
0
0
Inumber :        7550163
Size in bytes :  20
Segmentation fault

Segmentation error, I love this ones, usually mean rewrite your program/algorithm. Any usefull sugestions? What and where do I try to access or overwrite some important information?

Sorry, forgot thank you for help :o

Sorted out, found my mistake.

Thank you for your help again :cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.