Thread: Urgent C program help plz

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    15

    Exclamation Urgent C program help plz

    dear readers I have done my researches and still un-able to solve this program so please I need someone to help me or even solve it but as I think we must use semaphores, forks and pipes that's what I think but not understanding how to start am not good in C language... thanks for all.

    We are requested to implement a code cracking system. The system will deploy four processes (p1,p2,p3 & p4) to try to guess the password of an encrypted code.
    For encryption, we consider that the formula is: C = P XOR K, where
    • C = encrypted code
    • P = plain text
    • K = key
    For decryption the formula is: P = C XOR K. So, your program will try to find K using this formula. The XOR operator is ^.
    Given an encrypted code C and plain code P your program should deploy three processes to try and find the key used to encrypt the plain code. These three processes will be under the control of process P1.
    All processes will send their results to process P1 and process P1 will terminate the three processes and display the encryption key and decrypted text once any of the three processes finds the key.
    For simplicity we will consider that we have the original number P stored in a file called plain.txt and the encrypted code is stored in a file called cipher.txt. Consider that only numbers are stored in these files and they have a range between 0 and 100000000.
    So we have P and C your program should find the key K. Consider that K is a positive number between 0 and 1000000.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Tigerfady.

    We don't take program requests, however. If you want help with a C problem, post up the code you have (the doctors must see the patient), and include the relevant info: what's wrong, what error messages or warnings is it generating when you compile it, and any algorithmic info that is not obvious. (So we know HOW you are trying to do something, not just WHAT you're trying to do).

    The start of the program is always (well.... almost), entirely up to you. You are in the class, reading the book, etc., so show your stuff here, with a good start on it, at least.

    When you post code, be sure to use the code tags (highlight your code, and click on the code tag icon, in the advanced editor). That will make your code post as code, instead of all squished up html text. (which is very hard to study).

    It sounds like the assignment is to have the parent program generate three threads. If so, this is not a beginner's program, and you should be very well able to start the program off, no problem.

    You'll need to give the info on what to do with the 3 threads, if you want help with them.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Please show some work!!

    Read this link Announcements - General Programming Boards

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Not gonna happen, stahta01.

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    If that's your assignment, and it's urgent, and it's that urgent over Christmas, and you have not a line of code - honestly, just give up the course now. Seriously.

    What you are discussing is, what? A couple of hundred lines of code at best? To get a single-process version running will be a few dozen lines, if that. The rest is just applying "what you learned in the last tutorial", if standard educational procedures are anything to go by.

    What do you have? If you honestly have nothing but take the time to register and copy/paste this to several sites, you could have had a working version enough that we could help in the same time.

    I suggest you do your homework. Failing that, I suggest you drop or change your course. Because if this stumps you so much that you're posting for help on multiple sites so soon after Christmas with no idea how to proceed - well, you aren't going to get much further without a lot of work on your end anyway.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Apparently, according to the link provided by rags_to_riches, this problem is actually their "friends" issue, not theirs. ::snicker::

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Yeah, we're like doctors. We ALWAYS believe when the patient has a "friend" with a problem, especially if it's embarrassing...

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    dear doctors my problem is that I am not understanding the question not more ! what I am realizing is that I have to fork 4 processes and each one must be read into a buffer
    "which then writes it into the code and then specify which is the P and K "
    this quoted thing I am not finding out the way to do it ! I want to add that I know nothing about C programming language and those which I realized is what I have read !
    Last edited by tigerfady11; 12-26-2012 at 03:27 PM.

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    Code:
    .#include<stdio.h>
     #include<sys/types.h>
     #include<unistd.h>
     typedef struct decrypt
     {
     int p;
     int k;
    int c;
    c=p^k;
     }decrypt;
     int main()
     {
     decrypt s[10];
     int n,fd,i;
     fd=open("p.c",O_CREAT,777);//creates a new file
     if(fd<0)
     {
     printf("\nFailed to create file\n\n");exit(-1) 
     }
     fd=open("p.c",O_RDWR,777)//open the file for read and write
     printf("Enter the number to decrypt:");
     scanf("%d",&n);
     printf("Enter the key\n");
     for(i=0;i<n;i++0
     { 
     
     scanf("%d",&s[i].p);
     
     scanff("%d",&s[i].n);
     write( fd,s[i],sizeof(s) );//write to the file which the file discriptor is pointing to
     }
     
    return 0;//return success to kernel
     }
    //didn't know if I wrote it properly or even how to use semaphores  please someone help me in here
    .
    Last edited by tigerfady11; 12-26-2012 at 03:51 PM.

  10. #10
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by tigerfady11 View Post
    dear readers I have done my researches and still un-able to solve this program so please I need someone to help me or even solve it but as I think we must use semaphores, forks and pipes that's what I think but not understanding how to start am not good in C language... thanks for all.

    We are requested to implement a code cracking system. The system will deploy four processes (p1,p2,p3 & p4) to try to guess the password of an encrypted code.
    For encryption, we consider that the formula is: C = P XOR K, where
    • C = encrypted code
    • P = plain text
    • K = key
    For decryption the formula is: P = C XOR K. So, your program will try to find K using this formula. The XOR operator is ^.
    Given an encrypted code C and plain code P your program should deploy three processes to try and find the key used to encrypt the plain code. These three processes will be under the control of process P1.
    All processes will send their results to process P1 and process P1 will terminate the three processes and display the encryption key and decrypted text once any of the three processes finds the key.
    For simplicity we will consider that we have the original number P stored in a file called plain.txt and the encrypted code is stored in a file called cipher.txt. Consider that only numbers are stored in these files and they have a range between 0 and 100000000.
    So we have P and C your program should find the key K. Consider that K is a positive number between 0 and 1000000.
    I can't help but wonder if this user isn't in the same place where all the outsourcing is going, threatening real programmers jobs.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Sorry, but for someone posting such a random gibberish that barely looks like code, we cannot help you to "urgently" solve a problem such as this; which in all reality is going to take weeks to learn how to do, minimum. You've probably waited until way past when someone would have been able to help you!

    Don't worry, failing isn't a total loss, as long as you learn from it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just a suggestion from the peanut gallery (whose class did not include threads and semaphores): code up the program using functions, instead of threads, first. Get all the logic for the encryption and decryption working in the three functions.

    Once that's working correctly, then concentrate on the change over to a threaded model of the program. Tackling both at the same time, would be a bit much for me - but you've come a long way, for "someone who knows nothing about C", in a very short amount of time. Congrats.

    The code for the encryption is easy, but you haven't mentioned how you intend to decrypt the plain text, with any of the threads.

    Don't you want to use the newer stream file handlers in stdio.h, rather than the older low-level file handles in io.h? For example, fwrite(), instead of write(), etc.?

  13. #13
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    That code would never compile in its current state, and thus you've obviously never tried to compile it.

    Assignments inside struct definitions, stray characters, double-opening the same file immediately, functions that don't exist (scanff?).

    It's a bad copy/paste/guess how things might be with no testing or context or even basic C knowledge. And it also does precisely zero of what is asked of you (there is no "encryption" at all, unless you think that single invalid assignment inside the struct defition counts).

    I'm guessing that you're just THAT far behind that you have no idea what you're supposed to be doing, not having followed previous tutorials at all, not just one or two.

    Seriously, get it to the point where you can compile it, and we'll then help a little. Then you'll need to get it to the point where it *works* in some fashion (which is a totally different thing), and we'll then help a little more. Then you need to get a program that encrypts as specified, then we'll help a little more. Then you'll be CLOSE to getting an answer.

    At this rate, it's taken you - what - 24 hours to copy/paste some junk that doesn't even compile and contains absolute nonsense. It's going to take a LOT longer for you to get close to having something worthy of handing in. Depending on your schedule, I suggest you knuckle down and learn some C, or give up entirely.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  14. #14
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    I don't want to give up but I will tell you about the logic I came to think about sir... first I have to write a pipe in the parent process p1 which reads the plaintext and ciphertext containing numbers generated randomly (and is more easier if provided by user) between 0 and 100000000 then sends it throught another pipe after a fork to child process p2, then p2 must contain a function which transforms the decimal numbers in ciphertext and plaintext to binary since the XOR works bitwise ,then sends it throught another pipe after a fork to child process p3, then p3 must contain the function K=P^C which gets the key in binary if we have answer 1 then it is true else it is false , then sends the answer throught another pipe after a fork to child process p4, then p4 must contain a function which transforms the key again to decimal then resends it to P1 which displays the answers from all forks ... is that right ???? but what is a semaphore? and how do I use it in here ? and i think he said 3 trials ? trials for what ?

  15. #15
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    and dont forget the "Urgent C program help plz", which is what the "read here" says NOT to do!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-30-2012, 09:32 PM
  2. Need help with a turbo c program....urgent plz help
    By hassan_shohag in forum C Programming
    Replies: 10
    Last Post: 06-27-2010, 01:14 PM
  3. C Program help (urgent)
    By haris in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 04:24 AM
  4. Help needed with program - urgent thanks!
    By lildevil in forum C Programming
    Replies: 1
    Last Post: 03-09-2008, 06:45 AM
  5. urgent help for this difficult program
    By ptr_ritchie in forum C Programming
    Replies: 1
    Last Post: 04-01-2007, 01:42 AM