Jump to content
Byte-ul

[Mini] C challenge

Recommended Posts

Posted (edited)
34 minutes ago, Nytro said:

int main()
{
  if (ChuckNorris())
    printf("Muie ");
  else
    printf("Dragnea");
  
  return 0;
}

Era banal...

 

Nu cred ca toti stiu de "hrusca.h"

 

"Hrusca stie cat dureaza vesnica pomenire"

Edited by BogdanNBV
  • Active Members
Posted

Cel mai usor era:

 

int main()
{
  if (fork()) printf("Muie ");
  else printf("Dragnea");
}

Nu mai e nevoie de

return 0;

ca nu mai suntem in 2008.

  • Upvote 1
Posted
22 minutes ago, MrGrj said:

Cel mai usor era:

 


int main()
{
  if (fork()) printf("Muie ");
  else printf("Dragnea");
}

Nu mai e nevoie de


return 0;

ca nu mai suntem in 2008.

Bai garaj lasă c-ul ca ești paralel cu el :)) Ramai la Python. 

Posted (edited)
1 hour ago, MrGrj said:

 

Nu mai e nevoie de


return 0;

ca nu mai suntem in 2008.

Am citit si eu ceva asemanator intr-o carte de C++, a fost nasoala cartea..

1 hour ago, MrGrj said:

 

How come ? :)

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main(void) {
    pid_t child_pid;
    char *tasks[] = {"Task1", "Task2", "Task3", "Task4", "Task5"};
    const int task_no = 5;
    for (int i = 0; i < task_no; ++i) {
        child_pid = fork();
        if (child_pid == 0) { // Child
            printf("Child: I got to do %s\n", tasks[i]);
            _exit(i);
        } else { // Parrent
            printf("Parrent: Spawned child %d\n", i);
        }
    }
 
    printf("\nI'm going to wait for my children now.\n");
    printf("Hopefully they don't turn into zombies\n\n");
    int status = -1;
    for (int i = 0; i < task_no; ++i) {
        wait(&status);
        if (WIFEXITED(status)) {
            printf("Parrent: My child is not a zombie, yay.\n");
            printf("Parrent: Child %d finished, yay!\n", WEXITSTATUS(status));
        }
    }
    return 0;
}

 

Edited by Philip.J.Fry
  • Upvote 1
  • Active Members
Posted
3 minutes ago, Philip.J.Fry said:

Am citit si eu ceva asemanator intr-o carte de C++, a fost nasoala cartea..


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main(void) {
    pid_t child_pid;
    char *tasks[] = {"Task1", "Task2", "Task3", "Task4", "Task5"};
    const int task_no = 5;
    for (int i = 0; i < task_no; ++i) {
        child_pid = fork();
        if (child_pid == 0) { // Child
            printf("Child: I got to do %s\n", tasks[i]);
            _exit(i);
        } else { // Parrent
            printf("Parrent: Spawned child %d\n", i);
        }
    }
 
    printf("\nI'm going to wait for my children now.\n");
    printf("Hopefully they don't turn into zombies\n\n");
    int status = -1;
    for (int i = 0; i < task_no; ++i) {
        wait(&status);
        if (WIFEXITED(status)) {
            printf("Parrent: My child is not a zombie, yay.\n");
            printf("Parrent: Child %d finished, yay!\n", WEXITSTATUS(status));
        }
    }
    return 0;
}

 

 

Nu inteleg ce vrei sa demonstrezi cu postul de mai sus :) 

 

 

Posted (edited)
23 hours ago, MrGrj said:

 

How come ? :)

 

Am uitat sa-ti raspund. Compileaza aia si iti dai seama singur.

tl;dr unele standarde C au nevoie de return in main sau rezulta undefined behavior.

Edited by Byte-ul
Posted

Zygote performs a fork, creating a new process that is a clone of itself, drops privileges and sets its UID appropriately for the application’s sandbox, and finishes initialization of Dalvik in that process so that the Java runtime is fully executing. For example, it must start threads like the garbage collector after it forks.

Posted
On 3/22/2017 at 0:19 PM, gigiRoman said:

Zygote performs a fork, creating a new process that is a clone of itself, drops privileges and sets its UID appropriately for the application’s sandbox, and finishes initialization of Dalvik in that process so that the Java runtime is fully executing. For example, it must start threads like the garbage collector after it forks.

Ce treaba are Androidul cu challenge-ul asta ?

Posted
5 hours ago, Okjokes said:

Da? Care-s alea?

prima pui in if prima conditie negata , si a2a , egalezi prima conditie cu ce vrei tu 

if(!printf("Mue "))

if(printf("Mue ")==oricenumar)

Posted (edited)
6 hours ago, tjt said:

Ce treaba are Androidul cu challenge-ul asta ?

Cred ca e offtopic ce a scris el, dar merge compilat si pe Android, ca doar e linux (try "termux" and you get console).

 

6 hours ago, Okjokes said:

Da? Care-s alea?

Exista cel putin 2 metode (fork() - deja precizat de MrGrj sau vfork())

 

Am o curiozitate ce ma macina. Ar putea merge vreodata ceva ce seamana cu ceea ce e scris mai jos?

(Incerc sa rescriu "jmp" care apare in locul "else"-ului, in functia "main". Din cunostintele mele e o incercare nereusita, din cauza optimizarilor diferitelor compilatoare, dar.. ar putea merge ceva de genul macar pe un compilator, cu optiunile de compilare specificate? A treia conditie din "if" era o incercare de a pacali compilatorul ca pot folosi ambele ramuri, atat "if" cat si "else". Nu merge intotdeauna si uneori poate optimiza si sa dispara una din ramuri "if"/"else" la compilare. Pozitiile 39 si 40 nu sunt chiar random, sunt preluate cu un hexeditor (diferenta de la inceputul adresei functiei main pana la adresa "else" in asamblare, iar instructiunea "jmp" in cazul meu era pe 2 octeti asa ca am pus 2 NOP (0x90 pentru x86) ). Codul nu mi-a mers, am primit un "Segmentation fault (core dumped)". )

Spoiler

int main()
{
  if ( ((((char*)main)[39] = 0x90) == 0x90) &&
       ((((char*)main)[40] = 0x90) == 0x90) &&
       (((char*)main)[10] != 1) )
    printf("Muie ");
  else // aici face "jmp" la adresa la care se face "return 0;"; am incercat sa rescriu acest "jmp"
    printf("Dragnea");
  return 0;
}

 

Edited by a13x4nd7u
Adaugare explicatii
Posted (edited)
8 minutes ago, a13x4nd7u said:

Cred ca e offtopic ce a scris el, dar merge compilat si pe Android, ca doar e linux (try "termux" and you get console).

 

Exista cel putin 2 metode (fork() - deja precizat de MrGrj sau vfork())

 

Am o curiozitate ce ma macina. Ar putea merge vreodata ceva ce seamana cu ceea ce e scris mai jos?

(Incerc sa rescriu "jmp" care apare in locul "else"-ului, in functia "main". Din cunostintele mele e o incercare nereusita, din cauza optimizarilor diferitelor compilatoare, dar.. ar putea merge ceva de genul macar pe un compilator, cu optiunile de compilare specificate? A treia conditie din "if" era o incercare de a pacali compilatorul ca pot folosi ambele ramuri, atat "if" cat si "else". Nu merge intotdeauna si uneori poate optimiza si sa dispara una din ramuri "if"/"else" la compilare. )

  Hide contents

int main()
{
  if ( ((((char*)main)[39] = 0x90) == 0x90) &&
       ((((char*)main)[40] = 0x90) == 0x90) &&
       (((char*)main)[10] != 1) )
    printf("Muie ");
  else
    printf("Dragnea");
  return 0;
}

 

 

 

La asa ceva de genu' m-am gandit si eu, dar nu cred ca e posibil. 

Experienta mea e limitata in ASM, asa ca poate gresesc, dar cred ca in momentul in care ai facut jump nu poti sa mai faci return ci pleaca de la linia respectiva. 

Edited by tjt
  • Upvote 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...